Python利用api接口查询IP地址归属地
import requests import argparse import json def get_parameter(): parser=argparse.ArgumentParser(description=‘该脚本用于查看IP的归属地‘) parser.add_argument(‘-a‘,dest=‘ipaddr‘,type=str,default=‘‘,help=‘输入查询IP‘) args=parser.parse_args() ipaddr=args.ipaddr return ipaddr def get_json(ipaddr): url=‘http://ip-api.com/json/{}?lang=zh-CN‘.format(ipaddr) headers={ ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0‘, ‘Connection‘:‘keep-alive‘ } r=requests.get(url,timeout=15,headers=headers) r.close() result=r.content.decode() return result def main(): ipaddr=get_parameter() ip_str=get_json(ipaddr) ip_json=json.loads(ip_str) ip_country=ip_json[‘country‘] ip_city=ip_json[‘city‘] ip_query=ip_json[‘query‘] print(‘查询的IP:{}\t归属地为:{}\t{}‘.format(ip_query,ip_country,ip_city)) if __name__==‘__main__‘: main()
相关推荐
seanzed 2020-10-15
夜影风个人空间 2020-09-22
LittleCoder 2020-09-11
studentxyh 2020-08-03
zhanglao 2020-06-26
playis 2020-06-16
playis 2020-06-16
hwm 2020-06-14
weibingbingnet 2020-06-14
seanzed 2020-06-14
zhanglao 2020-06-14
hotlinhao 2020-06-14
GearUpCloud 2020-06-13
playis 2020-06-10
coolhty 2020-06-09
playis 2020-05-29
XuDanT 2020-05-28
llltaotao 2020-05-19