码迷,mamicode.com
首页 > 其他好文 > 详细

超好用的ip归属区域查询

时间:2018-08-02 20:54:41      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:usr   ima   exce   inf   text   cal   脚本   baidu   api   

源码如下:

#!/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归属区域查询

标签:usr   ima   exce   inf   text   cal   脚本   baidu   api   

原文地址:https://www.cnblogs.com/misswangxing/p/9409160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!