Python实现快捷得到亚太区任意国家的IP段
来源:互联网 发布:java计算圆的面积 编辑:程序博客网 时间:2024/05/02 03:11
通过APNIC的地址库得到数据,应该比较准确权威,且IP库每天都在更新中。
import osimport sysimport socketimport mathimport urllibimport urllib2import structimport tempfilelocal_file = os.path.dirname(tempfile.mktemp()) + '/apnic_ip.txt'def _usage(): print 'Get area ip Usage:' print '\t%s <area> [Options]\n' % os.path.basename(sys.argv[0]) print 'area example: hk,jp,cn,us,sg etc..' print 'Options:' print ' -ipv4\t\tGet ipv4 address, default.' print ' -ipv6\t\tGet ipv6 address.' print ' -all\t\tGet all address info.' print ' -new\t\tFrom internet get a new info.' print ' -mask\t\teg: x.x.x.x/24, default.' print ' -part\t\teg: x.x.x.x-x.x.x.x' print '\nExample:' print ' %s hk -new\t\t' % os.path.basename(sys.argv[0])def getApnicFile(): print 'Downloading files, please wait...' try: req = urllib2.Request('ftp://ftp.apnic.net/public/apnic/stats/apnic/delegated-apnic-latest') response = urllib2.urlopen(req) the_page = response.read() fs = open(local_file,'w') fs.write(the_page) fs.close() except: print '[-] Request data error!' return False return Truedef viewPartInfo(location,itype): fs = open(local_file,'r') for line in fs: if line[0] == '#': continue list = line.split('|') if list[1] != location : continue if itype != 3: if itype == 1: if list[2] != 'ipv4': continue elif itype == 2: if list[2] != 'ipv6': continue else: if list[2] != 'ipv4': continue else: if list[2] == 'asn': continue if list[2] == 'ipv6': print '%s/%s' % (list[3],list[4]) elif list[2] == 'ipv4': iStart = socket.ntohl(struct.unpack('I',socket.inet_aton(list[3]))[0]) + 1 iEnd = iStart + int(list[4]) - 3 print '%s-%s' % (socket.inet_ntoa(struct.pack('I',socket.htonl(iStart))),socket.inet_ntoa(struct.pack('I',socket.htonl(iEnd)))) fs.close() def viewMaskInfo(location,types): fs = open(local_file,'r') for line in fs: if line[0] == '#': continue list = line.split('|') if list[1] != location: continue if itype != 3: if itype == 1: if list[2] != 'ipv4': continue elif itype == 2: if list[2] != 'ipv6': continue else: if list[2] != 'ipv4': continue else: if list[2] == 'asn': continue if list[2] == 'ipv6': print '%s/%s' % (list[3],list[4]) elif list[2] == 'ipv4': print '%s/%d' % (list[3],32-int(math.log(int(list[4]),2))) fs.close()if __name__ == '__main__': argLen = len(sys.argv) if argLen < 2: _usage() sys.exit(0) arg = sys.argv single1 = 0 single2 = 0 itype = 1 iview = 1 for x in range(1,argLen): if arg[x] == '-ipv4': single1 += 1 itype = 1 if arg[x] == '-ipv6': single1 += 1 itype = 2 if arg[x] == '-all': single1 += 1 itype = 3 if arg[x] == '-new': if not getApnicFile(): print '[-] Download new ip file failed!' pass if arg[x] == '-mask': single2 += 1 iview = 1 if arg[x] == '-part': single2 += 1 iview = 2 if single1 > 1: print '[-] ipv4,ipv6,all only select one.\n' sys.exit(0) if single2 > 1: print '[-] mask,part only select one.\n' sys.exit(0) if not os.path.exists(local_file): if not getApnicFile(): sys.exit(0) location = sys.argv[1].upper() if iview == 2: viewPartInfo(location,itype) else: viewMaskInfo(location,itype)
更新其他国家地区IP段下载地址:
ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latestftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latestftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latestftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latestftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest
0 0
- Python实现快捷得到亚太区任意国家的IP段
- 通过ip得到国家的ip库安装以及使用
- Python实现ip段地址
- python得到本地网卡的IP(linux)
- python得到本地网卡的IP
- 得到真实外网IP、IP所在国家、省份、地区
- 得到真实外网IP、IP所在国家、省份、地区
- 得到真实外网IP、IP所在国家、省份、地区
- 获取联通、电信、任意ISP ip段
- 获取联通、电信、任意ISP ip段
- php禁止单IP、IP段访问的实现代码
- python-----奇怪的国家
- 任意大数字加法的python实现
- python简易实现任意位数的水仙花
- python获取指定IP国家代码信息
- python取IP C段
- php 实现 过滤国家ip访问功能
- ip段的理解
- 软交所胡才勇获“2014中国软件和信息服务十大领军人物”
- 基于 OGRE 粒子系统的喷泉模拟
- Android简单封装类似JQuery异步请求
- git-安装、配置与使用
- opencv的牛人博客
- Python实现快捷得到亚太区任意国家的IP段
- 获取当前系统时间
- 安卓实现在百度地图上播放历史轨迹
- CSS实现兼容性的渐变背景(gradient)效果
- 【CF】 159 D Palindrome pairs
- GCC 编译详解
- Little study of Setting Windows Schedule job
- System.getProperty()常用值
- hadoop 2.6 编译