{"id":380,"date":"2021-08-24T10:07:00","date_gmt":"2021-08-24T10:07:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=380"},"modified":"2021-08-24T10:07:00","modified_gmt":"2021-08-24T10:07:00","slug":"380","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/24\/380\/","title":{"rendered":"\u514d\u8d39\u7684IP\u5b9a\u4f4d \u9ad8\u5fb7\u4e91\u56fe\u79d1\u6280 \u8bd5\u7528\u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<h2>IP\u5b9a\u4f4d  \u5c06IP\u4fe1\u606f\u8f6c\u6362\u4e3a\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f  0\u5143\uff0f100000\u6b21 <a href=\"https:\/\/market.aliyun.com\/products\/57002002\/cmapi018957.html\">\u8d2d\u4e70\u94fe\u63a5<\/a><\/h2>\n<h3>Python 2.X \u7684API \u63a5\u53e3\u793a\u4f8b<\/h3>\n<pre><code>import urllib, urllib2, sys\n\nhost = 'http:\/\/iploc.market.alicloudapi.com'\npath = '\/v3\/ip'\nmethod = 'GET'\nappcode = '\u4f60\u81ea\u5df1\u7684AppCode'\nquerys = 'ip=114.247.50.2'\nbodys = {}\nurl = host + path + '?' + querys\n\nrequest = urllib2.Request(url)\nrequest.add_header('Authorization', 'APPCODE ' + appcode)\nresponse = urllib2.urlopen(request)\ncontent = response.read()\nif (content):\n    print(content)<\/code><\/pre>\n<h3>Python 3.X \u4f7f\u7528\u9700\u8981\u4fee\u6539 API \u63a5\u53e3<\/h3>\n<pre><code>import urllib, sys , json\nimport urllib.request\n\nhost = 'http:\/\/iploc.market.alicloudapi.com'\npath = '\/v3\/ip'\nmethod = 'GET'\nappcode = '\u4f60\u81ea\u5df1\u7684AppCode'\nquerys = 'ip=218.72.183.139'\nbodys = {}\nurl = host + path + '?' + querys\n# url =  'http:\/\/iploc.market.alicloudapi.com\/v3\/ip?ip=218.72.183.139'\n\nrqs = urllib.request.Request(url)\nrqs.add_header('Authorization', 'APPCODE ' + appcode)\nresponse = urllib.request.urlopen(rqs)\ncontent = response.read()\nif (content):\n    print(content)\n    data = json.loads(content)\n    print(data)<\/code><\/pre>\n<h3>\u6b63\u5e38\u8fd4\u56de\u793a\u4f8b \u6b63\u786e\u7684 <code>json<\/code> \u6570\u636e<\/h3>\n<pre><code>b'{\"status\":\"1\",\"info\":\"OK\",\"infocode\":\"10000\",\"province\":\"xe6xb5x99xe6xb1x9fxe7x9cx81\",\"city\":\"xe9x87x91xe5x8dx8exe5xb8x82\",\"adcode\":\"330700\",\"rectangle\":\"119.5281601,28.9855063;119.7682178,29.16913797\"}'\n{'status': '1', 'info': 'OK', 'infocode': '10000', 'province': '\u6d59\u6c5f\u7701', 'city': '\u91d1\u534e\u5e02', 'adcode': '330700', 'rectangle': '119.5281601,28.9855063;119.7682178,29.16913797'}<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3905235763.png\" alt=\"ip.png\" title=\"ip.png\"><\/p>\n<h3>\u5982\u679c\u4f60\u5df2\u7ecf\u8d2d\u4e70\u514d\u8d39\u7684IP\u5b9a\u4f4d\uff0c\u53ef\u4ee5\u5728\u6587\u6863\u9875\u9762\u70b9\u51fb \u8c03\u8bd5 \u6309\u94ae\uff0c\u67e5\u770b\u8c03\u8bd5\u4fe1\u606f<\/h3>\n<pre><code>Request\uff1a\nUrl: http:\/\/iploc.market.alicloudapi.com\/v3\/ip?ip=114.247.50.2\nHeader: {\"Authorization\":\"APPCODE 74 '\u4f60\u81ea\u5df1\u7684AppCode' 52\",\n\"X-Ca-Timestamp\":\"1629798898253\",\"gateway_channel\":\"http\",\"X-Ca-Key\":\"203980786\"\n,\"x-ca-nonce\":\"b30d861c-7503-4449-8df0-07ce759afb1c\",\n\"X-Ca-Request-Mode\":\"DEBUG\",\"X-Ca-Stage\":\"RELEASE\",\n\"Host\":\"iploc.market.alicloudapi.com\",\"Content-Type\":\"application\/json; charset=utf-8\"}<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3440328139.png\" alt=\"Header.png\" title=\"Header.png\"><\/p>\n<ul>\n<li>\u53ef\u4ee5\u770b\u5230 <code>.add_header('Authorization', 'APPCODE ' + appcode)<\/code> \u662f\u6dfb\u52a0\u4e00\u4e2a\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>requests<\/code> \u5e93\u628aAPI \u63a5\u53e3\u5199\u7684\u66f4\u52a0\u7cbe\u7b80\uff0c\u73b0\u5728\u5c31\u662f\u8fd8\u4e0d\u77e5\u9053\uff0c\u6539\u5199\u540e\u662f\u5426\u5ef6\u8fdf\u52a0\u5927<\/li>\n<li>\n<p>\u4f7f\u7528 <code>requests<\/code> \u5e93\u7248\u672c\uff0c\u4e5f\u80fd\u8fd4\u56de\u6b63\u786e\u7684 json \u4fe1\u606f<\/p>\n<pre><code>import requests, json\nurl = 'http:\/\/iploc.market.alicloudapi.com\/v3\/ip?ip=218.72.183.139'\nheaders = {\"Authorization\":\"APPCODE  \u4f60\u81ea\u5df1\u7684AppCode \" ,\"Content-Type\":\"application\/json; charset=utf-8\" }\nr = requests.get(url=url , headers=headers)\ndata = r.json()\nprint(data)<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3321232979.png\" alt=\"requests.png\" title=\"requests.png\"><\/p>\n<\/li>\n<\/ul>\n<h3>Pytyhon \u4f7f\u7528\u767e\u5ea6\u5730\u56feAPI \u8fdb\u884c IP\u666e\u901a\u5b9a\u4f4d \u548c \u9ad8\u5fb7\u514d\u8d39\u5b9a\u4f4d \u901f\u5ea6\u6bd4\u8f83\uff0c\u5b9e\u5728\u6709\u70b9\u6162<\/h3>\n<h3>\u6240\u4ee5\u628a <code>app.py<\/code> <a href=\"https:\/\/github.com\/hongwenjun\/ip\/blob\/main\/app.py\">\u6e90\u7801<\/a> \u6539\u6210\u4e86 \u9ad8\u5fb7\u5b9a\u4f4d\u7684API\u63a5\u53e3<\/h3>\n<pre><code>def ip2bdgps(ip):\n    url = 'https:\/\/api.map.baidu.com\/location\/ip?ak=&lt;&lt;AK\u7801&gt;&gt;&amp;coor=bd09ll&amp;ip=' + ip\n    r = requests.get(url)\n    json_str = r.text\n    data = json.loads(json_str)\n\n    if data['status'] != 0 :\n        return  (116.39564504, 39.92998578 , data['status'])    # \u67e5\u4e0d\u5230\u8fd4\u56de \u5317\u4eac x,y\n    else:\n        x = data['content']['point']['x']\n        y = data['content']['point']['y']\n    return  (x, y, data['status'], data)<\/code><\/pre>\n<pre><code>def ip2gdgps(ip):\n    url = 'http:\/\/iploc.market.alicloudapi.com\/v3\/ip?ip=' + ip\n    headers = {\"Authorization\":\"APPCODE &lt;&lt;\u5b9e\u9645appcode&gt;&gt;\" ,\"Content-Type\":\"application\/json; charset=utf-8\" }\n    try:\n        r = requests.get(url=url , headers=headers)\n        data = r.json()\n    except :\n        return\n    # print(data)\n    if data['status'] != '1':\n        return  (116.39564504, 39.92998578 , data['status'])    # \u67e5\u4e0d\u5230\u8fd4\u56de \u5317\u4eac x,y\n    elif data['rectangle']:\n        rectangle = data['rectangle']   # '119.5281601,28.9855063;119.7682178,29.16913797'\n        arry = rectangle.replace(';', ',').split(',')\n        x = (float(arry[0]) +  float(arry[2])) \/ 2.0\n        y = (float(arry[1]) +  float(arry[3])) \/ 2.0\n        return  (x, y, data['status'], data)\n    return  (116.39564504, 39.92998578 , data['status'])    # \u67e5\u4e0d\u5230\u8fd4\u56de \u5317\u4eac x,y<\/code><\/pre>\n<h3>\u6f14\u793aURL  <a href=\"https:\/\/www.lyvba.com\/ip\/maps\/\">https:\/\/www.lyvba.com\/ip\/maps\/<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>IP\u5b9a\u4f4d \u5c06IP\u4fe1\u606f\u8f6c\u6362\u4e3a\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f 0\u5143\uff0f100000\u6b21 \u8d2d\u4e70\u94fe\u63a5 Python 2.X \u7684AP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-380","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=380"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/380\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}