第一步:购买接口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)
结果: