使用阿里接口查询IP归属地

第一步:购买接口https://www.aliyun.com/ss/?k=%E5%85%A8%E7%90%83IP%E5%BD%92%E5%B1%9E%E5%9C%B0%E6%9F%A5%E8%AF%A2

第二步:我是用这个接口,购买后进入控制台找APPCODE

第三步:

import requests
from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED
headers = {
    "Authorization":'APPCODE '+'阿里接口上面APPCODE '
}
#批量查找
def Check_ip():
    with open('source_ip.txt','r',encoding='utf-8') as f:    #source_ip.txt查找的IP地址文件
        ip_list=f.readlines()
    for ip in ip_list:
        ip=ip.split('\n')[0]
        url=f'https://ips.market.alicloudapi.com/iplocaltion?ip={ip}'
        print(url)
        req=requests.get(url,headers=headers)
        if req.status_code==200:
            json_data=req.json()
            country=json_data['result']['nation']
            province=json_data['result']['province']
            city=json_data['result']['city']
            with open('result.txt','a') as f:    #将结果保存到result.txt
                f.write(ip+':'+country+' '+province+' '+city+'\n')
                print('check ip secceed ! %s:%s %s %s'%(ip,country,province,city))
        else:
            httpReason = req.headers['X-Ca-Error-Message']
            if (req.status_code == 400 and httpReason == 'Invalid Param Location'):
                print("参数错误")
            elif (req.status_code == 400 and httpReason == 'Invalid AppCode'):
                print("AppCode错误")
            elif (req.status_code == 400 and httpReason == 'Invalid Url'):
                print("请求的 Method、Path 或者环境错误")
            elif (req.status_code == 403 and httpReason == 'Unauthorized'):
                print("服务未被授权(或URL和Path不正确)")
            elif (req.status_code == 403 and httpReason == 'Quota Exhausted'):
                print("套餐包次数用完")
            elif (req.status_code == 500):
                print("API网关错误")
            else:
                print("参数名错误 或 其他错误")
                print(req.status_code)
                print(httpReason)

if __name__ == '__main__':
    max_process = ThreadPoolExecutor(max_workers=10)
    tasks = [max_process.submit(Check_ip)]
    wait(tasks, return_when=ALL_COMPLETED)

结果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值