python 通过ip获取城市_python 通过ip 地址获取真实地址 使用纯真数据库

该博客介绍了如何使用Python通过纯真IP数据库来获取IP地址对应的城市和真实地址。通过解析数据库文件,实现从IP到字符串的转换,并提供了一个`IpLocater`类,用于查找并返回IP地址的详细地理位置信息。
摘要由CSDN通过智能技术生成

python 通过ip 地址获取真实地址 使用纯真数据库

1.[代码][Python]代码

# -*- coding: utf-8 -*-

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__" :

fiel_name = 'D:/web/www/tools/ipdata/QQWry.dat'

ip_locater = IpLocater(fiel_name )

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值