做资产盘点或者信息搜集时经常需要查询IP的的位置和运营商,故造轮子。
使用接口http://ip.taobao.com
- 优点:查询返回json格式字符串,便于处理,对新手友好
- 缺点:无法使用域名查询
# 查询IP
import requests
def cha(ip):
url = 'http://ip.taobao.com/outGetIpInfo'
headers = {
'Accept':'*/*',
'X-Requested-With':'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Origin':'http://ip.taobao.com',
'Referer':'http://ip.taobao.com/ipSearch',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9'
}
payload = {'ip':ip,'accessKey':'alibaba-inc'}
res = requests.post(url,headers=headers,data=payload)
di12 = res.json()
guojia = di12['data']['country']
sheng = di12['data']['region']
chengshi = di12['data']['city']
ip = di12['data']['ip']
yys = di12['data']['isp']
print(di12['msg'])
# print(type(res.text))
# return res.json()
return ip+"[+]"+guojia+sheng+chengshi+" 运营商:"+yys
if __name__ == "__main__":
# s1 = str(input("请输入IP: "))
# filename = str(input("File name: "))
filename = 'IP_LIST.txt' #ip列表文件
file1 = open(filename,'r')
try:
for line in file1:
ip = line.strip()
print(cha(ip))
except:
print('error')
## for line in file1:
## if len(line) != 0:
## ip = line.strip()
## print(cha(ip))
## else:
## print('空')
file1.close()
# print(cha(s1))