首页 > 编程语言 > 详细

批量获取IP的位置,ISP脚本(python)

时间:2020-07-06 22:05:23      阅读:80      评论:0      收藏:0      [点我收藏+]

做资产盘点或者信息搜集时经常需要查询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))

批量获取IP的位置,ISP脚本(python)

原文:https://www.cnblogs.com/NBeveryday/p/13257840.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!