{"id":231,"date":"2024-01-10T20:33:01","date_gmt":"2024-01-10T12:33:01","guid":{"rendered":"https:\/\/wangqianming.top\/?p=231"},"modified":"2024-01-10T20:39:04","modified_gmt":"2024-01-10T12:39:04","slug":"python%e5%ae%9e%e7%8e%b0web%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/wangqianming.top\/index.php\/2024\/01\/10\/python%e5%ae%9e%e7%8e%b0web%e6%9c%8d%e5%8a%a1%e5%99%a8\/","title":{"rendered":"python\u5b9e\u73b0web\u670d\u52a1\u5668"},"content":{"rendered":"<h1>python\u5b9e\u73b0\u7b80\u5355web\u670d\u52a1<\/h1>\n<p>\u6211\u4eec\u6709\u65f6\u8981\u8fdc\u7a0b\u542f\u52a8\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u7684\u4e00\u4e2a\u811a\u672c\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528python\u6765\u5199\u4e00\u4e2a\u7b80\u5355\u7684web\u670d\u52a1\u5668\uff0c\u8fd9\u6bb5python\u670d\u52a1\u5668\u4ee3\u7801\u653e\u5728\u88ab\u8c03\u7528\u7684\u8fdc\u7a0b\u7535\u8111\u4e0a\uff0c\u8fd0\u884c\u8be5web\u670d\u52a1\uff0c\u9700\u8981\u8c03\u7528\u65f6\uff0c\u4f7f\u7528\u672c\u5730\u7535\u8111\u53d1\u9001web\u8bf7\u6c42\u5373\u53ef\u3002\uff08\u8981\u6c42\u672c\u5730\u7535\u8111\u80fdping\u901a\u8fdc\u7a0b\u7535\u8111\uff09<\/p>\n<p>web\u670d\u52a1\u5668\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from flask import Flask, request, jsonify\n\napp = Flask(__name__)\n\n@app.route(&#039;\/invoke_function&#039;, methods=[&#039;POST&#039;])\ndef invoke_function():\n    try:\n        # \u83b7\u53d6 POST \u8bf7\u6c42\u7684 JSON \u6570\u636e\n        data = request.get_json()\n\n        # \u4ece JSON \u6570\u636e\u4e2d\u63d0\u53d6\u53c2\u6570\n        param1 = data.get(&#039;param1&#039;)\n        param2 = data.get(&#039;param2&#039;)\n\n        # \u8c03\u7528\u4f60\u7684 Python \u51fd\u6570\n        result = your_function(param1, param2)\n\n        # \u8fd4\u56de\u7ed3\u679c\n        return jsonify({&#039;result&#039;: result})\n\n    except Exception as e:\n        return jsonify({&#039;error&#039;: str(e)})\n\n# \u4f60\u7684 Python \u51fd\u6570\ndef your_function(param1, param2):\n    # \u5728\u8fd9\u91cc\u5b9e\u73b0\u4f60\u7684\u529f\u80fd\n    # \u4f8b\u5982\uff0c\u53ef\u4ee5\u662f\u67d0\u4e2a\u8ba1\u7b97\u3001\u5904\u7406\u7b49\n    return param1 + param2\n\nif __name__ == &#039;__main__&#039;:\n    # \u542f\u52a8 Flask \u5e94\u7528\n    app.run(host=&#039;0.0.0.0&#039;, port=5000)\n<\/code><\/pre>\n<p>\u4ee3\u7801\u7b80\u5355\u6613\u61c2\uff0c\u5728\u6b64\u4e0d\u4f5c\u8fc7\u591a\u8bf4\u660e\uff0c\u9700\u8981\u7684python\u5305flask\uff0c\u81ea\u884c\u5b89\u88c5\u3002<br \/>\n\u8fd0\u884c\u4e4b\u540e\u4f1a\u5728\u670d\u52a1\u5668\u6bb5\u7684ip\u5730\u5740\u76845000\u7aef\u53e3\u8fd0\u884c\u4e00\u4e2aweb\u670d\u52a1\u5668\u3002<\/p>\n<p>\u4e4b\u540e\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u7684\u7535\u8111\u4e0a\u7f16\u5199python\u4ee3\u7801\u6765\u53d1\u9001web\u8bf7\u6c42\uff0c\u4ee5\u6b64\u6765\u9a8c\u8bc1\u662f\u5426\u80fd\u8fdc\u7a0b\u8c03\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &#039;http:\/\/172.31.226.104:5000\/invoke_function&#039;\ndata = {&#039;param1&#039;: 10, &#039;param2&#039;: 20}\nresponse = requests.post(url, json=data)\n\nprint(response.json())\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684url\u4e2d\u7684ip\u5730\u5740\u5e94\u8be5\u6539\u4e3a\u4f60\u81ea\u5df1\u7684\u670d\u52a1\u5668\u7aef\u7684IP\u5730\u5740\u3002<br \/>\n\u8fd0\u884c\u4e4b\u540e\uff0c\u4e0d\u51fa\u610f\u5916\u7684\u8bdd\u4f1a\u8f93\u51fa<code>{&#039;result&#039;: 30}<\/code>\uff0c\u8fd9\u4e2a\u662f10+20\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u5b9e\u73b0\u7b80\u5355web\u670d\u52a1 \u6211\u4eec\u6709\u65f6\u8981\u8fdc\u7a0b\u542f\u52a8\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u7684\u4e00\u4e2a\u811a\u672c\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528python\u6765\u5199\u4e00\u4e2a\u7b80\u5355\u7684web\u670d\u52a1 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[24,25],"class_list":["post-231","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python","tag-web"],"_links":{"self":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":2,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/231\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}