超好用的ip归属区域查询
Posted misswangxing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了超好用的ip归属区域查询相关的知识,希望对你有一定的参考价值。
源码如下:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
from bs4 import BeautifulSoup
import re
import IPy
def check_ip(Ip):
url = ‘https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?co=&resource_id=6006&t=1529895387942&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110203920624944751099_1529894588086&_=1529894588088&query=‘ + Ip
headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/67.0.3396.87 Safari/537.36‘}
r = requests.get(url, headers=headers)
r.encoding = r.apparent_encoding
html = r.text
c1 = html.split(‘location":"‘)[1]
c2 = c1.split(‘",‘)[0]
return c2
#判断ip地址是否正确
def is_ip(address):
try:
IPy.IP(address)
return True
except Exception as e:
return False
# 获取外网IP
def get_out_ip():
#url1 = ‘https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?co=&resource_id=6006&t=1529895387942&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110203920624944751099_1529894588086&_=1529894588088&query=‘ + Ip
#headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36‘}
url = ‘http://ip.42.pl/raw‘
r = requests.get(url)
result = r.text
return result
#检查IP地址归属地
def checkIP():
Ip = input("Please input a IP(按回车键退出):")
judge = is_ip(Ip)
if judge:
length = len(Ip.split(‘.‘))
if length == 4:
c2 = check_ip(Ip)
result = Ip + ‘来自‘ + c2
return result
elif Ip.strip() == ‘‘:
exit(2)
# return (‘IP输入错误,请重新输入!‘)
else:
return (‘Ip输入错误,请重新输入!‘)
if __name__ == ‘__main__‘:
Ip = get_out_ip()
print(‘本机IP:‘ + Ip + ‘ ‘ + check_ip(Ip))
while True:
print(checkIP())
可以打包成windwos的可执行exe格式,通过软件pyinstaller软件效果如下图:
此处为单个ip查询,也可以自行修改脚本同时查询多个。
以上是关于超好用的ip归属区域查询的主要内容,如果未能解决你的问题,请参考以下文章