{"id":342,"date":"2021-08-19T06:39:00","date_gmt":"2021-08-19T06:39:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=342"},"modified":"2021-08-19T06:39:00","modified_gmt":"2021-08-19T06:39:00","slug":"342","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/19\/342\/","title":{"rendered":"Python\u7f51\u7edc\u5f00\u53d1\u7b80\u5355\u7684IP\u57ce\u5e02\u5b9a\u4f4dWebAPI"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/352652374.jpg\" alt=\"py_ip.jpg\" title=\"py_ip.jpg\"><\/p>\n<h1>Python\u7f51\u7edc\u5f00\u53d1\u7b80\u5355\u7684IP\u57ce\u5e02\u5b9a\u4f4dWebAPI<\/h1>\n<ul>\n<li>\u4e4b\u524d\u5b66\u4e60Python\uff0c\u5df2\u7ecf\u5b66\u4f1a\u4e86\u4f7f\u7528IP\u548c\u67e5\u8be2\u6240\u5728\u56fd\u5bb6\u548c\u57ce\u5e02\uff0c\u4f7f\u7528 <code>ipip-ipdb<\/code> \u5e93\u548cgithub\u4e0a\u5206\u4eab\u7684IP\u5730\u5740\u5e93\u8fd8\u662f\u633a\u7b80\u5355\u7684\u3002<\/li>\n<li>\u5982\u679c\u4f60\u8fd8\u60f3\u4e86\u89e3 <code>ipip-ipdb<\/code> \u5e93\u4f7f\u7528\uff0c\u53ef\u4ee5\u7b49\u4e0b\u518d\u770b <a href=\"https:\/\/www.lyvba.com\/index.php\/archives\/249\/\">\u94fe\u63a5<\/a> \u300aPython\u811a\u672c: \u7edf\u8ba1\u8bbf\u95eeNginx\u7684\u524d100\u4e2aIP\u548c\u67e5\u8be2\u6240\u5728\u56fd\u5bb6\u548c\u57ce\u5e02\u300b<\/li>\n<\/ul>\n<h2>WebAPI\u6f14\u793a:  <a href=\"https:\/\/lyvba.com\/ip\/\">https:\/\/lyvba.com\/ip\/<\/a><\/h2>\n<ul>\n<li><a href=\"https:\/\/lyvba.com\/ip\/\">https:\/\/lyvba.com\/ip\/<\/a>          # \u83b7\u5f97\u6d4f\u89c8\u5668\u8bbf\u95ee IP \u548c \u5b9a\u4f4d<\/li>\n<li><a href=\"https:\/\/lyvba.com\/ip\/8.8.8.8\">https:\/\/lyvba.com\/ip\/8.8.8.8<\/a>   # \u67e5\u8be2\u81ea\u5b9a\u4e49 IP \u5b9a\u4f4d<\/li>\n<li><a href=\"https:\/\/lyvba.com\/ip\/github.com\">https:\/\/lyvba.com\/ip\/github.com<\/a>   # \u6309\u57df\u540d\u67e5 IP \u5b9a\u4f4d<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1753374019.png\" alt=\"getip.png\" title=\"getip.png\"><\/p>\n<h3>\u73b0\u5728\u6765\u8bf4\u660e Python\u7f51\u7edc\u5f00\u53d1 \u7684\u90e8\u7f72\uff0c\u90e8\u7f72\u6210\u529f\u80fd\u5982\u4e0b\u56fe\uff0c\u8f93\u5165 <code>curl<\/code> \u547d\u4ee4\u83b7\u5f97\u6b63\u786e\u7684IP\u5b9a\u4f4d<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/2926440293.png\" alt=\"ip.png\" title=\"ip.png\"><\/p>\n<h2>\u9996\u5148\u7b2c\u4e00\u6b65\uff0c\u5b89\u88c5 <code>\"Flask<\/code> \u548c <code>ipip-ipdb<\/code> \u4e24\u4e2a\u6240\u9700\u5e93<\/h2>\n<ul>\n<li>\n<p>\u5982\u679c\u4f60\u7535\u8111\u6216\u8005\u670d\u52a1\u5668 \u5df2\u7ecf\u5b89\u88c5\u6709 <code>Python3<\/code>\uff0c\u53ea\u9700\u4e0b\u9762\u4e00\u884c\u547d\u4ee4\u5c31\u80fd\u5b89\u88c5<\/p>\n<pre><code>pip3  install Flask  ipip-ipdb<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1016263906.png\" alt=\"111.png\" title=\"111.png\"><\/p>\n<ul>\n<li>\u6211\u5b9e\u9645\u662f\u90e8\u7f72\u5728\u535a\u5ba2\u670d\u52a1\u5668\u4e0a\uff0c\u6240\u4ee5\u6211\u4f7f\u7528 <code>Docker<\/code> \u5bb9\u5668\u65b9\u5f0f\u90e8\u7f72\uff0c\u8fd9\u6837\u7684\u597d\u5904\u662f\u4e0d\u4f1a\u628a\u670d\u52a1\u5668\u641e\u5d29\u6e83\u548c\u5220\u9664\u6e05\u7406\u4e5f\u5f88\u7b80\u5355<\/li>\n<\/ul>\n<h3>\u5982\u679c\u4f60\u4e5f\u4f7f\u7528 <code>Docker Python3<\/code> \u65b9\u5f0f\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u8bd5\u7528\u6211\u7684\u5bb9\u5668\u65b9\u6cd5<\/h3>\n<pre><code># \u90e8\u7f72 Docker Python3 \u5bb9\u5668\u5728\u540e\u53f0\u8fd0\u884c\ndocker run -d  -p 5000:5000 --restart=always   \\r\n    -v \/app:\/app  --name python3 \\r\n    hongwenjun\/python3  \\r\n    python3 -m http.server 8000\n\n# \u8fdb\u5165\u5bb9\u5668 \u5b89\u88c5 Flask  ipip-ipdb\u5e93\ndocker exec -it python3 bash\npip3  install Flask  ipip-ipdb\n<\/code><\/pre>\n<h2>\u63a5\u4e0b\u7b2c\u4e8c\u6b65\uff0c\u4e0b\u8f7d QQ\u7eaf\u771fIP\u5730\u5740\u5e93\uff0c\u548c <code>app.py<\/code> \u7684\u6e90\u7801<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3826227713.png\" alt=\"app.png\" title=\"app.png\"><\/p>\n<pre><code>cd \/app\nwget https:\/\/cdn.jsdelivr.net\/npm\/qqwry.ipdb\/qqwry.ipdb\nwget https:\/\/raw.githubusercontent.com\/hongwenjun\/srgb\/master\/python\/app.py<\/code><\/pre>\n<h2>\u90e8\u7f72\u5df2\u7ecf\u5b8c\u6210\uff0c\u7b2c\u4e09\u6b65\u53ea\u8981\u628a IP\u57ce\u5e02\u5b9a\u4f4dWebAPI \u8fd0\u884c\u8d77\u6765\u5c31\u53ef\u4ee5\u4e86<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1914734069.png\" alt=\"tmux.png\" title=\"tmux.png\"><\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528 tmux \u628a flask run \u7a0b\u5e8f\u4fdd\u6301\u5230\u540e\u53f0\u8fd0\u884c<\/p>\n<pre><code>tmux -u\nflask run --host=0.0.0.0<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>\u4f7f\u7528 <code>curl<\/code> \u6d4b\u8bd5 WebAPI \u80fd\u591f\u6b63\u786e\u4f7f\u7528<\/h2>\n<pre><code># curl 172.17.0.3:5000\n&lt;p&gt;Hello, World!&lt;\/p&gt;\n\n# curl 172.17.0.3:5000\/ip\/8.8.8.8\n8.8.8.8 @\u7f8e\u56fd\u52a0\u5229\u798f\u5c3c\u4e9a\u5dde\u5723\u514b\u62c9\u62c9<\/code><\/pre>\n<h2>\u63a5\u4e0b\u5c31\u4f7f\u7528 nginx \u53cd\u5411\u4ee3\u7406\u7aef\u53e3\u914d\u7f6e\uff0c\u4f7f\u7528\u6548\u679c\u8fd8\u4e0d\u9519<\/h2>\n<pre><code>location \/ip\/ {\n    proxy_pass http:\/\/172.17.0.3:5000\/ip\/;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n    proxy_set_header X-Forwarded-Proto $scheme;\n    proxy_set_header X-Forwarded-Protocol $scheme;\n    proxy_set_header X-Forwarded-Host $http_host;\n\n\n    proxy_buffering off;\n}<\/code><\/pre>\n<h2><code>app.py<\/code> \u6e90\u7801\u521d\u59cb\u7248  <a href=\"https:\/\/raw.githubusercontent.com\/hongwenjun\/srgb\/master\/python\/app.py\">\u6700\u65b0\u7248<\/a><\/h2>\n<pre><code>import  ipdb, ipaddress\nfrom flask import Flask\n# export FLASK_ENV=development\n# flask run --host=0.0.0.0\n\ndb = ipdb.BaseStation(\"qqwry.ipdb\")\napp = Flask(__name__)\n\n@app.route(\"\/\")\ndef hello_world():\n    return \"&lt;p&gt;Hello, World!&lt;\/p&gt;\"\n\n@app.route('\/ip\/&lt;ipaddr&gt;')\ndef show_ip_profile(ipaddr):\n    ip = ipaddr\n    try:\n        ipaddress.ip_address(ip).is_global\n        # print(ip, db.find(ip, \"CN\"))\n        city = db.find(ip, \"CN\")\n        ipaddr =  ip + \" @\" + city[0] + city[1] + city[2] + \"\n\"\n    except:\n        pass\n    return ipaddr\n<\/code><\/pre>\n<h2><code>app.py<\/code> \u6e90\u7801\u53e6\u4e00\u79cd\u7248\u672c<\/h2>\n<pre><code>import ipdb, ipaddress\nfrom flask import Flask, request, jsonify\n\ndb = ipdb.BaseStation(\"qqwry.ipdb\")\napp = Flask(__name__)\n\n@app.route(\"\/ip\/\")\n@app.route(\"\/ip\/&lt;ipaddr&gt;\")\ndef show_ip(ipaddr=None):\n    # ip \u5730\u5740\u4e3a\u7a7a\u83b7\u5f97\u6d4f\u89c8\u5668\u5ba2\u6237\u7aefIP\n    if ipaddr is None:\n        ip = request.remote_addr\n        try:\n            _ip = request.headers[\"X-Real-IP\"]\n            if _ip is not None:\n                ip = _ip\n        except Exception as e:\n            print(e)\n    else:\n        ip = ipaddr\n\n    # ip\u5730\u5740 \u4ece\u7eaf\u771fIP\u6570\u636e\u5e93 \u641c\u7d22\u57ce\u5e02\u5b9a\u4f4d\n    try:\n        ipaddress.ip_address(ip).is_global\n        city = db.find(ip, \"CN\")\n        ipaddr = ip + \" @\" + city[0] + city[1] + city[2] + city[3] + \"\n\"\n    except Exception as e:\n        print(e)\n\n    return ipaddr\n\n# export FLASK_ENV=development   # \u8c03\u8bd5\u6a21\u5f0f: \u4fee\u6539\u4ee3\u7801\u4e0d\u7528\u91cd\u542f\u670d\u52a1\n# flask run --host=0.0.0.0       # \u76d1\u542c\u6240\u6709\u516c\u5f00\u7684 IP<\/code><\/pre>\n<h2><code>app.py<\/code> \u6e90\u7801 \u652f\u6301\u57df\u540d\u67e5 IP\u5b9a\u4f4d<\/h2>\n<pre><code>import ipdb, ipaddress\nfrom flask import Flask, request, jsonify\nfrom socket import gethostbyname\n\ndb = ipdb.BaseStation(\"qqwry.ipdb\")\napp = Flask(__name__)\n\ndef iplocated(ip):\n    city = db.find(ip, \"CN\")\n    return ip + \" @\" + city[0] + city[1] + city[2] + city[3] + \"\n\"\n\n@app.route(\"\/ip\/\")\n@app.route(\"\/ip\/&lt;ipaddr&gt;\")\ndef show_ip(ipaddr=None):\n    # ip \u5730\u5740\u4e3a\u7a7a\u83b7\u5f97\u6d4f\u89c8\u5668\u5ba2\u6237\u7aefIP\n    if ipaddr is None:\n        ip = request.remote_addr\n        try:\n            _ip = request.headers[\"X-Real-IP\"]\n            if _ip is not None:\n                ip = _ip\n        except Exception as e:\n            print(e)\n    else:\n        ip = ipaddr\n\n    # ip\u5730\u5740 \u4ece\u7eaf\u771fIP\u6570\u636e\u5e93 \u641c\u7d22\u57ce\u5e02\u5b9a\u4f4d\n    try:\n        ipaddress.ip_address(ip).is_global\n        ipaddr = iplocated(ip)\n    except:\n        try:\n            ip = gethostbyname(ip)     # \u57df\u540d\u53cd\u89e3\u6790\u5f97\u5230\u7684IP\n            ipaddr = iplocated(ip)\n        except Exception as e:\n            print(e)\n\n    return ipaddr\n\n# export FLASK_ENV=development   # \u8c03\u8bd5\u6a21\u5f0f: \u4fee\u6539\u4ee3\u7801\u4e0d\u7528\u91cd\u542f\u670d\u52a1\n# flask run --host=0.0.0.0       # \u76d1\u542c\u6240\u6709\u516c\u5f00\u7684 IP\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7f51\u7edc\u5f00\u53d1\u7b80\u5355\u7684IP\u57ce\u5e02\u5b9a\u4f4dWebAPI \u4e4b\u524d\u5b66\u4e60Python\uff0c\u5df2\u7ecf\u5b66\u4f1a\u4e86\u4f7f\u7528IP\u548c\u67e5\u8be2\u6240 [&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-342","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\/342","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=342"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}