700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python查询ip归属地_基于Python的IP地址归属地查询

python查询ip归属地_基于Python的IP地址归属地查询

时间:2022-01-06 14:21:49

相关推荐

python查询ip归属地_基于Python的IP地址归属地查询

一、开通接口

IP地址归属地查询服务使用聚合数据提供的免费接口,每天可以100次免费调用。可以通过 /docs/api/id/1 注册及开通。

二、请求接口

#!/usr/bin/python

# -*- coding: utf-8 -*-

import urllib, urllib2, sys, json

reload(sys)

sys.setdefaultencoding('utf-8')

url = '/ip/ipNew'

params = {

"ip": "58.215.15.114", # 查询的IP地址

"key": "******", # 您申请的接口API接口请求Key

}

querys = urllib.urlencode(params)

request = urllib2.Request(url, data=querys)

response = urllib2.urlopen(request)

content = response.read()

if (content):

try:

result = json.loads(content)

error_code = result['error_code']

if (error_code == 0):

country = result['result']['Country']

province = result['result']['Province']

city = result['result']['City']

isp = result['result']['Isp']

print("国家:%s\n省份:%s\n城市:%s\n运营商:%s" % (country, province, city, isp))

else:

print("请求失败:%s %s" % (result['error_code'], result['reason']))

except Exception as e:

print("解析结果异常:%s" % e)

else:

# 可能网络异常等问题,无法获取返回内容,请求异常

print("请求异常")

三、请求结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。