python判断地址是否相同,python 判断IP 网段

  python判断地址是否相同,python 判断IP 网段

  两种方法1、geolite22、外部接口3、资源下载

  1、geolite2

  有三份文件要准备。

  后两者相当于小型数据库,里面已经存储了ip和地理信息,我们可以写代码查询。

  python中的代码如下

  进口重新导入地理IP 2。数据库读取器=geo IP 2。数据库。读者( geo lite 2-城市。MMDB )#查询互联网协议(互联网协议)地址对应的物理地址定义IP _获取_位置(IP _地址):#载入指定互联网协议(互联网协议)相关数据response=reader.city(ip地址)#读取国家代码country _ iso代码=响应。国家。iso _代码#读取国家名称country _ Name=响应。国家。姓名#读取国家名称(中文显示)Country _ name CN=响应。国家。姓名[ zh-CN ] #读取州(国外)/省(国内)名称国别名称=响应。细分。最具体的。姓名#读取州(国外)/省(国内)代码特定国家iso代码=响应。细分。最具体的。iso _代码#读取城市名称City_Name=response.city.name #读取邮政编码城市_邮政编码=响应。邮政。代码#获取纬度位置_纬度=响应。位置。纬度#获取经度位置_经度=响应。位置。经度if(Country _ iso code==None):Country _ iso code= None if(Country _ Name==None):Country _ Name cn= None if(Country _ specific Name==None):Country _ specific Name= None if(Country _ specific iso code==None):Country _ specific iso code= None if(City _ Name==None):City _ Name= None if(City _ post code==None) target: IP _ address geolite 2-Located )print(u []国家编码: Country_IsoCode) print(u [ ]国家名称: Country_Name) print(u [ ]国家中文名称: Country_NameCN) print(u [ ]省份或州名称:特定于国家/地区的名称)print(u []省份或州编码:特定国家iso代码)打印(u []城市名称: City_Name) print(u [ ]城市邮编: City_PostalCode) print(u [ ]纬度: str(Location _ Latitude))print(u []经度: str(Location _ Longitude))print(================End=================)#检验和处理互联网协议(互联网协议的缩写)地址def separate _ IP(IP _ address):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)$ 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)$ if re.match(ip_match,IP _ address):try:IP _ get _ location(IP _ address)Exception as e:print(e)elif re匹配(IP _ list,IP _ address):IP _ start=IP _ address。拆分(-)[0].拆分(.)[3]IP _ end=IP _ address。拆分(-)[1]#如果互联网协议(互联网协议的缩写)地址范围一样,则直接执行if(IP _ start==IP _ end):try:separate _ IP(IP _ address。split(-)[0])异常为e:print(e)elif IP _ start IP _ end:print(您输入的互联网协议(Internet Protocol)的值已经错误!"再试一次!")exit(0)else:IP _ num _ list=IP _ address。拆分(-)[0].拆分(.)IP _ num _ list。范围内IP _ last的pop()(int(IP _ start),int(ip_end) 1): ip_add= . 。join(ip_num_list) . str(IP _ last)try:IP _ get _ location(IP _ add)Exception as e:print(e)else:print(错误的互联网协议(Internet Protocol)地址类型!)打印( 100.8.11.58 100.8.11.58-100相似!)if _ _ name _ _== _ _ main _ _ :separate _ IP( 113。248 .151 .48 )当然这不是我写的,从哪儿来的也忘了,但亲测可用

  不过也有问题,就是不准确,不信的可以试试

  2、外部接口另一个替代方案是使用其他网站作为接口,每次就用爬虫邮政查询,源码如下

  bs4的导入请求进口美丽的汤。248 .151 .48 网址=https://www.ip138.com/iplookup.asp?ip={}action=2 .format(IP)headers={ User-Agent : Mozilla/5.0(Windows NT 10.0;WOW64) AppleWebKit/537.36 (KHTML,像壁虎一样)Chrome/80。0 .3987 .100 Safari/537.36 } #获取响应response=requests.get(url=url,headers=headers)响应。编码= GB 2312 html=响应。回复中匹配的文本。finditer((prov city CT):(.*?)"",html): print(match.group())

  类似地你就可以把它嵌入在你的网站里了

  打个补丁,可以把结果放到字典里供查询

  bs4的导入请求进口美丽的汤。248 .151 .48 网址=https://www.ip138.com/iplookup.asp?ip={}action=2 .format(IP)headers={ User-Agent : Mozilla/5.0(Windows NT 10.0;WOW64) AppleWebKit/537.36 (KHTML,像壁虎一样)Chrome/80。0 .3987 .100 Safari/537.36 } #获取响应response=requests.get(url=url,headers=headers)响应。编码= GB 2312 html=响应。textinfo=dict()用于re中的匹配。finditer((prov city CT):(.*?)"",html): divide=match.group().拆分(:)信息[{} 。格式(除[0]。strip( )]=divide[1].条带("")打印(信息)

  3、资源下载验卫星

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: