使用python根据ip获取目标地理位置信息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用python根据ip获取目标地理位置信息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4207字,纯文字阅读大概需要7分钟。
内容图文
![使用python根据ip获取目标地理位置信息](/upload/InfoBanner/zyjiaocheng/1204/ea88236698014a1589cd87f1394a08bd.jpg)
信息安全很重要,你的地理位置可能暴露了!!!
使用python和GeoLite2获取目标的地理位置
1 # ! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 ‘‘‘ 5 Created on 2019年12月8日 6 7 @author: Admin 8 ‘‘‘ 9 10 from copy import copy 11import optparse 12import re 13 14import geoip2.database 15 16 17 reader = geoip2.database.Reader(‘GeoLite2-City.mmdb‘) 18 19# 查询IP地址对应的物理地址 20def ip_get_location(ip_address): 21# 载入指定IP相关数据 22 response = reader.city(ip_address) 23 24#读取国家代码 25 Country_IsoCode = response.country.iso_code 26#读取国家名称 27 Country_Name = response.country.name 28#读取国家名称(中文显示) 29 Country_NameCN = response.country.names[‘zh-CN‘] 30#读取州(国外)/省(国内)名称 31 Country_SpecificName = response.subdivisions.most_specific.name 32#读取州(国外)/省(国内)代码 33 Country_SpecificIsoCode = response.subdivisions.most_specific.iso_code 34#读取城市名称 35 City_Name = response.city.name 36#读取邮政编码 37 City_PostalCode = response.postal.code 38#获取纬度 39 Location_Latitude = response.location.latitude 40#获取经度 41 Location_Longitude = response.location.longitude 42 43if(Country_IsoCode == None): 44 Country_IsoCode = "None" 45if(Country_Name == None): 46 Country_Name = "None" 47if(Country_NameCN == None): 48 Country_NameCN = "None" 49if(Country_SpecificName == None): 50 Country_SpecificName = "None" 51if(Country_SpecificIsoCode == None): 52 Country_SpecificIsoCode = "None" 53if(City_Name == None): 54 City_Name = "None" 55if(City_PostalCode == None): 56 City_PostalCode = "None" 57if(Location_Latitude == None): 58 Location_Latitude = "None" 59if(Location_Longitude == None): 60 Location_Longitude = "None" 61 62print(‘================Start===================‘) 63print(‘[*] Target: ‘ + ip_address + ‘ GeoLite2-Located ‘) 64print(u‘ [+] 国家编码: ‘ + Country_IsoCode) 65print(u‘ [+] 国家名称: ‘ + Country_Name) 66print(u‘ [+] 国家中文名称: ‘ + Country_NameCN) 67print(u‘ [+] 省份或州名称: ‘ + Country_SpecificName) 68print(u‘ [+] 省份或州编码: ‘ + Country_SpecificIsoCode) 69print(u‘ [+] 城市名称 : ‘ + City_Name) 70print(u‘ [+] 城市邮编 : ‘ + City_PostalCode) 71print(u‘ [+] 纬度: ‘ + str(Location_Latitude)) 72print(u‘ [+] 经度 : ‘ + str(Location_Longitude)) 73print(‘===============End======================‘) 74 75# 检验和处理ip地址 76def seperate_ip(ip_address): 77 ip_match = r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)\.)(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)$" 78 ip_match_list = r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)\.)(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9])-(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)$" 79 80if re.match(ip_match, ip_address): 81try: 82 ip_get_location(ip_address) 83except Exception as e: 84print e 85elif re.match(ip_match_list, ip_address): 86 ip_start = ip_address.split(‘-‘)[0].split(‘.‘)[3] 87 ip_end = ip_address.split(‘-‘)[1] 88# 如果ip地址范围一样,则直接执行 89if(ip_start == ip_end): 90try: 91 seperate_ip(ip_address.split(‘-‘)[0]) 92except Exception as e: 93print e 94elif ip_start > ip_end: 95print‘the value of ip, that you input, has been wrong! try again!‘ 96 exit(0) 97else: 98 ip_num_list = ip_address.split(‘-‘)[0].split(‘.‘) 99 ip_num_list.pop() 100for ip_last in range(int(ip_start), int(ip_end)+1): 101 ip_add = ‘.‘.join(ip_num_list)+‘.‘+str(ip_last) 102try: 103 ip_get_location(ip_add) 104except Exception as e: 105print e 106else: 107print‘Wrong type of ip address!‘108print‘100.8.11.58 100.8.11.58-100 alike!‘109110# 主方法数体111def main(): 112 parser = optparse.OptionParser(‘Usage%prog -i <single ip address> -I <multi ip address> \n \n{ Egg: python getIpLocation.py -i(I) 100.8.11.58(100.8.11.58-100) }‘) 113 parser.add_option(‘-i‘, dest=‘SIp‘,type=‘string‘, help=‘specify pcap filename‘) 114 parser.add_option(‘-I‘, dest=‘MIp‘,type=‘string‘, help=‘specify pcap filename‘) 115 (options, args) = parser.parse_args() 116if options.SIp == None and options.MIp == None: 117print (parser.usage) 118 exit(0) 119 ip_address = options.SIp 120 ip_address_multi = options.MIp 121if ip_address==None: 122 seperate_ip(ip_address_multi) 123else: 124 seperate_ip(ip_address) 125126if__name__ == ‘__main__‘: 127 main()
结果:
原文:https://www.cnblogs.com/perilong16/p/12008715.html
内容总结
以上是互联网集市为您收集整理的使用python根据ip获取目标地理位置信息全部内容,希望文章能够帮你解决使用python根据ip获取目标地理位置信息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。