代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
Python 读取纯真IP数据库
作者:
Sephiroth
/ 发布于
2011/1/10
/
742
<div>!/usr/bin/python from struct import * import string def ip2string( ip ): a = (ip & 0xff000000) >> 24 b = (ip & 0x00ff0000) >> 16 c = (ip & 0x0000ff00) >> 8 d = ip & 0x000000ff return "%d.%d.%d.%d" % (a,b,c,d) def string2ip( str ): ss = string.split(str, '.'); ip = 0L for s in ss: ip = (ip << 8) + string.atoi(s) return ip; class IpLocater : def __init__( self, ipdb_file ): self.ipdb = open( ipdb_file, "rb" ) # get index address str = self.ipdb.read( 8 ) (self.first_index,self.last_index) = unpack('II',str) self.index_count = (self.last_index - self.first_index) / 7 + 1 def getString(self,offset = 0): if offset : self.ipdb.seek( offset ) str = "" ch = self.ipdb.read( 1 ) (byte,) = unpack('B',ch) while byte != 0: str = str + ch ch = self.ipdb.read( 1 ) (byte,) = unpack('B',ch) return str def getLong3(self,offset = 0): if offset : self.ipdb.seek( offset ) str = self.ipdb.read(3) (a,b) = unpack('HB',str) return (b << 16) + a def getAreaAddr(self,offset=0): if offset : self.ipdb.seek( offset ) str = self.ipdb.read( 1 ) (byte,) = unpack('B',str) if byte == 0x01 or byte == 0x02: p = self.getLong3() if p: return self.getString( p ) else: return "" else: return self.getString( offset ) def getAddr(self,offset ,ip = 0): self.ipdb.seek( offset + 4) countryAddr = "" areaAddr = "" str = self.ipdb.read( 1 ) (byte,) = unpack('B',str) if byte == 0x01: countryOffset = self.getLong3() self.ipdb.seek(countryOffset ) str = self.ipdb.read( 1 ) (b,) = unpack('B',str) if b == 0x02: countryAddr = self.getString( self.getLong3() ) self.ipdb.seek( countryOffset + 4 ) else: countryAddr = self.getString( countryOffset ) areaAddr = self.getAreaAddr() elif byte == 0x02: countryAddr = self.getString( self.getLong3() ) areaAddr = self.getAreaAddr( offset + 8 ) else: countryAddr = self.getString( offset + 4 ) areaAddr = self.getAreaAddr( ) return countryAddr + "/" + areaAddr def output(self, first ,last ): if last > self.index_count : last = self.index_count for index in range(first,last): offset = self.first_index + index * 7 self.ipdb.seek( offset ) buf = self.ipdb.read( 7 ) (ip,of1,of2) = unpack("IHB",buf) print "%s - %s" % (ip, self.getAddr( of1 + (of2 << 16) ) ) def find(self,ip,left,right): if right-left == 1: return left else: middle = ( left + right ) / 2 offset = self.first_index + middle * 7 self.ipdb.seek( offset ) buf = self.ipdb.read( 4 ) (new_ip,) = unpack("I",buf) if ip <= new_ip : return self.find( ip, left, middle ) else: return self.find( ip, middle, right ) def getIpAddr(self,ip): index = self.find( ip,0,self.index_count - 1 ) ioffset = self.first_index + index * 7 aoffset = self.getLong3( ioffset + 4) address = self.getAddr( aoffset ) return address if __name__ == "__main__" : ip_locater = IpLocater( "QQWry.Dat" ) ip_locater.output( 100, 120 ) ip = '59.64.234.174' ip = '58.38.139.229' address = ip_locater.getIpAddr( string2ip( ip ) ) print "the ip %s come from %s" % (ip,address)</div>
试试其它关键字
IP数据库
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
Sephiroth
贡献的其它代码
(
13
)
.
Python 最长公共子串算法
.
Python 126邮箱自动登录程序
.
Python 实现enum的功能
.
Python viterbi算法
.
将阿拉伯数字转换为罗马数字
.
Python 写入数据到MP3文件中
.
Python 调用默认浏览器
.
Python 使用xlrd读取Excel格式文件
.
PycURL 自动处理cookie
.
PycURL 实现POST方法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3