在网上找的脚本,已经验证过,可以使用,需要重新指定库文件,大家用的时候,需要注意下。

ip.py

#!/usr/bin/env python
#fileencoding=utf-8
import struct
import socket
class Node(object):
__slots__ = ('ip', 'country', 'district')
def __init__(self, ip, country, district):
self.ip = ip
self.country = country
self.district = district
def __str__(self):
return unicode(self).encode('utf-8')
def __unicode__(self):
return u'IPRecord(ip: {}; country: {}; district: {})'.format(
socket.inet_ntoa(struct.pack("!L", self.ip)),
self.country, self.district)
def parse_long_4(bytes_):
return struct.unpack('<L', bytes_)[0]
def parse_long_3(bytes_):
return struct.unpack('<L', bytes_ + '\x00')[0]
def parse_char(bytes_):
return struct.unpack('<b', bytes_)[0]
def read_c_string(data, begin):
end = begin
while data[end] != '\x00':
end += 1
# exclude trailing \0
return data[begin: end].decode('gbk', 'replace'), end + 1
def read_string(data, begin):
mode = parse_char(data[begin: begin + 1])
if mode == 2:
offset = parse_long_3(data[begin + 1: begin + 4])
return read_c_string(data, offset)[0], begin + 4
else:
return read_c_string(data, begin)
def parse_record(data, begin):
ip = parse_long_4(data[begin: begin + 4])
mode = parse_char(data[begin + 4: begin + 5])
begin = begin + 5
if mode == 1:
record_begin = parse_long_3(data[begin: begin + 3])
country, record_begin = read_string(data, record_begin)
district, _ = read_string(data, record_begin)
elif mode == 2:
begin -= 1
country, record_begin = read_string(data, begin)
district, _ = read_string(data, record_begin)
else:
begin -= 1
country, record_begin = read_c_string(data, begin)
district, _ = read_string(data, record_begin)
return Node(ip, country, district)
def parse(data):
ips = []
index_offset_first, index_offset_last = \
parse_long_4(data[0: 4]), parse_long_4(data[4: 8])
while index_offset_first <= index_offset_last:
record = parse_record(data, parse_long_3(
data[index_offset_first + 4: index_offset_first + 7]))
ips.append(record)
index_offset_first += 7
return ips
class IP2Location(object):
def __init__(self, filename):
with open(filename, 'rb') as f:
self.ips = parse(f.read())
for ip in self.ips:
if ip.district == u' CZ88.NET':
ip.district = u''
if self.ips[-1].ip == 0xffffffffL:
self.ips[-1].country = u'IANA保留地址'
self.ips[-1].district = u''
def get_location(self, ip):
if isinstance(ip, str):
ip = struct.unpack('!L', socket.inet_aton(ip))[0]
ips = self.ips
low = 0
len_ = len(ips)
# lower_bound
while len_ > 0:
half_len = len_ / 2
mid = low + half_len
if ips[mid].ip < ip:
low = mid + 1
len_ = len_ - half_len - 1
else:
len_ = half_len
assert low < len(ips)
return ips[low]
class IP2LocationMe(object):
'''
memory efficient
'''
def __init__(self, filename):
with open(filename, 'rb') as f:
self.data = f.read()
self.index_begin, index_end = \
parse_long_4(self.data[0: 4]), parse_long_4(self.data[4: 8]) + 7
self.index_len = (index_end - self.index_begin) / 7
def get_location(self, ip):
if isinstance(ip, str):
ip = struct.unpack('!L', socket.inet_aton(ip))[0]
data = self.data
low = self.index_begin
len_ = self.index_len
# lower_bound
while len_ > 0:
half_len = len_ / 2
mid = low + half_len * 7
offset = parse_long_3(data[mid + 4: mid + 7])
mid_ip = parse_long_4(data[offset: offset + 4])
if mid_ip < ip:
low = mid + 7
len_ = len_ - half_len - 1
else:
len_ = half_len
record = parse_record(data, parse_long_3(
data[low + 4: low + 7]))
if record.district == u' CZ88.NET':
record.district = u''
if record.ip == 0xffffffff:
record.country = u'IANA保留地址'
record.district = u''
return record
def get_location(ip):
import os
self_dir = os.path.dirname(os.path.abspath(__file__))
client = IP2LocationMe(os.path.join(self_dir, 'ip_to_location.dat'))
globals()['get_location'] = client.get_location
return client.get_location(ip)
def main():
try:
while True:
ip = raw_input('ip:')
loc = get_location(ip)
print hex(loc.ip), loc.country, loc.district
except EOFError:
pass
if __name__ == '__main__':
main()

Python 脚本查询 ip纯真数据库相关推荐

  1. python ipaddr库_用Python脚本查询纯真IP库QQWry.dat(Demon修改版)

    #!/usr/bin/env python # coding: utf-8 # from: http://linuxtoy.org/files/pyip.py # Blog: http://linux ...

  2. 使用Python批量查询ip地址的信息

    使用Python批量查询ip地址的信息 import requests from openpyxl import load_workbook from bs4 import BeautifulSoup ...

  3. 编写Python脚本查询最近七天或者最近1天的百度收录量

    最近需要每天提交查询一批网站的百度最近7天的收录量,网站现在还不确定数量,可能比较多.编写Python脚本,定时自动运行,将结果存入excel或者mysql数据库都可以.在网上也找了相关的代码,但是大 ...

  4. python批量查询ip归属地_python3.2批量查询IP地址区域

    import urllib.request #jk409  2014-7-31 by python3.4编写用于[批量]查询IP地址国家区域 def chaxun(ip_addr): ip_addr2 ...

  5. oracle实例查询IP,Oracle数据库中获取访问者的IP地址或主机名的方法

    有时我们需要在Oracle数据库中获取访问者的局域网或Internet网的主机名或IP地址.在Oracle中可以使用下面的方法来实现: Oracle包utl_inaddr 作用:用于取得局域网或Int ...

  6. 简易 Python 脚本查询嵊泗船票

    夏天来了,这颗躁动的心啊,想去嵊泗玩几天~ 现在上海去嵊泗要上微信公众号或者官网买票,工作日还好,但是周末了不太容易抢到票了,又不能没事就刷手机(这太没有程序员范儿了). 所以,看看能不能用 Pyth ...

  7. python爬虫查询IP地址的归属地

    1.首先是借用ip地址查询网站www.ip138.com网站 我们在www.ip138.com网站中输入一个IP地址,就会弹出来相关的IP地址归属地信息 2.书写我们要访问的链接 >>&g ...

  8. 帆软报表调用python脚本_直连数据库实时更新数据,比python强大,可视化报表这么做简直牛...

    会数据库,会python,会做报表,到底有多吃香?我趁着周五没事,打开了某招聘,不到10分钟,竟然有好多HR让我换工作,不是说现在基本上不招人吗?是我太优秀了吗? 甚至还有不止一个跟我说报表+决策分析 ...

  9. mysql索引抽密度_使用python脚本从abaqus输出数据库获取元素密度

    属性关联如下:sectionAssignment将section连接到set set是{}的容器 section将sectionAssignment连接到material instance连接到{}( ...

  10. PHP 读取 QQ ip纯真数据库 详解

    占位编辑 转载于:https://www.cnblogs.com/xyxiong/archive/2011/02/18/1957986.html

最新文章

  1. 有时候明明没有问题的程序为什么通不过?
  2. 检索数据_19_按照字符串对结果排序
  3. WeihanLi.Npoi 1.20.0 Released
  4. 【51单片机快速入门指南】8:看门狗定时器(WDT)
  5. 安卓9.0添加服务修改SELinux
  6. apache支持mysql ubuntu_Ubuntu+Apache+PHP+Mysql环境搭建(完整版)
  7. grid安装前检查报PRVE-0426:/dev/shm less than the require
  8. MySQL数据库迁移注意点
  9. 倡议书格式范文_倡议书写作格式及范文(共9篇)
  10. boot camp驱动下载以及对应机型版本查询
  11. Objective C 获取当前日期时间方法
  12. Unable to boot device due to insufficient system resources
  13. python导入包如果找不到
  14. excel使用技巧手记
  15. 【文学与历史】浅谈戏说华夏历史
  16. Winsock API编程之UDP小结
  17. 3月16日----3月20日一年级课程表
  18. android马甲包代理,安卓渠道马甲包配置
  19. DB2相关数据库命令
  20. MTK65XX平台充电调试总结 (转载)

热门文章

  1. 小甲鱼 C语言 22课指针和数组
  2. 定时器 - 延时函数
  3. 怎么使用水经注万能地图下载器制作百度行政区划个性化地图
  4. xlsx 解析excel 后渲染到表格里(前端实现 解析excel渲染到表格)
  5. 简单论坛的搭建(Discuz)、memchche、OpenResty(类nginx)
  6. linux wqy字体安装,给openSUSE安装文泉驿字体
  7. 免费的科研论文画图软件drawio以及Mermaid
  8. 高校成绩管理数据库系统
  9. 重磅!Spring Cloud 生态再添新套件:Spring Cloud Tencent
  10. ROS安装教程(ubuntu16.04安装ROS Kinetic)