使用阿里接口查询IP归属地
第一步:购买接口
第二步:我是用这个接口,购买后进入控制台找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(
)[0]
url=fhttps://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+
)
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)
结果:
