蘭雅sRGB 个人笔记 https://262235.xyz
提供编程和电脑应用视频教程,工具和源代码
C, C++, Python Programming, Source Code, Video

旧Hexo博客 | Github | IP定位WebAPI | Docker Hub
编程中文文档 | 网盘分享 | 中文Linux命令

免费的IP定位 高德云图科技 试用学习笔记

IP定位 将IP信息转换为地理位置信息 0元/100000次 购买链接

Python 2.X 的API 接口示例

import urllib, urllib2, sys

host = 'http://iploc.market.alicloudapi.com'
path = '/v3/ip'
method = 'GET'
appcode = '你自己的AppCode'
querys = 'ip=114.247.50.2'
bodys = {}
url = host + path + '?' + querys

request = urllib2.Request(url)
request.add_header('Authorization', 'APPCODE ' + appcode)
response = urllib2.urlopen(request)
content = response.read()
if (content):
    print(content)

Python 3.X 使用需要修改 API 接口

import urllib, sys , json
import urllib.request

host = 'http://iploc.market.alicloudapi.com'
path = '/v3/ip'
method = 'GET'
appcode = '你自己的AppCode'
querys = 'ip=218.72.183.139'
bodys = {}
url = host + path + '?' + querys
# url =  'http://iploc.market.alicloudapi.com/v3/ip?ip=218.72.183.139'

rqs = urllib.request.Request(url)
rqs.add_header('Authorization', 'APPCODE ' + appcode)
response = urllib.request.urlopen(rqs)
content = response.read()
if (content):
    print(content)
    data = json.loads(content)
    print(data)

正常返回示例 正确的 json 数据

b'{"status":"1","info":"OK","infocode":"10000","province":"\xe6\xb5\x99\xe6\xb1\x9f\xe7\x9c\x81","city":"\xe9\x87\x91\xe5\x8d\x8e\xe5\xb8\x82","adcode":"330700","rectangle":"119.5281601,28.9855063;119.7682178,29.16913797"}'
{'status': '1', 'info': 'OK', 'infocode': '10000', 'province': '浙江省', 'city': '金华市', 'adcode': '330700', 'rectangle': '119.5281601,28.9855063;119.7682178,29.16913797'}

ip.png

如果你已经购买免费的IP定位,可以在文档页面点击 调试 按钮,查看调试信息

Request:
Url: http://iploc.market.alicloudapi.com/v3/ip?ip=114.247.50.2
Header: {"Authorization":"APPCODE 74 '你自己的AppCode' 52",
"X-Ca-Timestamp":"1629798898253","gateway_channel":"http","X-Ca-Key":"203980786"
,"x-ca-nonce":"b30d861c-7503-4449-8df0-07ce759afb1c",
"X-Ca-Request-Mode":"DEBUG","X-Ca-Stage":"RELEASE",
"Host":"iploc.market.alicloudapi.com","Content-Type":"application/json; charset=utf-8"}

Header.png

  • 可以看到 .add_header('Authorization', 'APPCODE ' + appcode) 是添加一个字典,我们可以使用 requests 库把API 接口写的更加精简,现在就是还不知道,改写后是否延迟加大
  • 使用 requests 库版本,也能返回正确的 json 信息

    import requests, json
    url = 'http://iploc.market.alicloudapi.com/v3/ip?ip=218.72.183.139'
    headers = {"Authorization":"APPCODE  你自己的AppCode " ,"Content-Type":"application/json; charset=utf-8" }
    r = requests.get(url=url , headers=headers)
    data = r.json()
    print(data)

    requests.png

Pytyhon 使用百度地图API 进行 IP普通定位 和 高德免费定位 速度比较,实在有点慢

所以把 app.py 源码 改成了 高德定位的API接口

def ip2bdgps(ip):
    url = 'https://api.map.baidu.com/location/ip?ak=<<AK码>>&coor=bd09ll&ip=' + ip
    r = requests.get(url)
    json_str = r.text
    data = json.loads(json_str)

    if data['status'] != 0 :
        return  (116.39564504, 39.92998578 , data['status'])    # 查不到返回 北京 x,y
    else:
        x = data['content']['point']['x']
        y = data['content']['point']['y']
    return  (x, y, data['status'], data)
def ip2gdgps(ip):
    url = 'http://iploc.market.alicloudapi.com/v3/ip?ip=' + ip
    headers = {"Authorization":"APPCODE <<实际appcode>>" ,"Content-Type":"application/json; charset=utf-8" }
    try:
        r = requests.get(url=url , headers=headers)
        data = r.json()
    except :
        return
    # print(data)
    if data['status'] != '1':
        return  (116.39564504, 39.92998578 , data['status'])    # 查不到返回 北京 x,y
    elif data['rectangle']:
        rectangle = data['rectangle']   # '119.5281601,28.9855063;119.7682178,29.16913797'
        arry = rectangle.replace(';', ',').split(',')
        x = (float(arry[0]) +  float(arry[2])) / 2.0
        y = (float(arry[1]) +  float(arry[3])) / 2.0
        return  (x, y, data['status'], data)
    return  (116.39564504, 39.92998578 , data['status'])    # 查不到返回 北京 x,y

演示URL https://www.262235.xyz/ip/maps/

本原创文章自由转载,转载请注明本博来源及网址 | 当前页面:兰雅sRGB个人笔记 » 免费的IP定位 高德云图科技 试用学习笔记