{"id":1026215,"date":"2024-12-31T10:40:44","date_gmt":"2024-12-31T02:40:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026215.html"},"modified":"2024-12-31T10:40:46","modified_gmt":"2024-12-31T02:40:46","slug":"%e5%be%ae%e4%bf%a1%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python%e7%bc%96%e7%9a%84%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1026215.html","title":{"rendered":"\u5fae\u4fe1\u5982\u4f55\u8c03\u7528python\u7f16\u7684\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0e61418e-1c28-4aab-8f17-7afa8ec068bc.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5fae\u4fe1\u5982\u4f55\u8c03\u7528python\u7f16\u7684\u7a0b\u5e8f\" \/><\/p>\n<p><p> \u5fae\u4fe1\u5982\u4f55\u8c03\u7528Python\u7f16\u7684\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>\u5728\u5fae\u4fe1\u4e2d\u8c03\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u5fae\u4fe1\u516c\u4f17\u53f7\u3001\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u3001\u7b2c\u4e09\u65b9\u5f00\u53d1\u5e73\u53f0\u3001\u4f7f\u7528Web\u63a5\u53e3\u7b49\u65b9\u5f0f\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5fae\u4fe1\u516c\u4f17\u53f7\u548c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u53f7\u548c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8c03\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5fae\u4fe1\u516c\u4f17\u53f7<\/h3>\n<\/p>\n<p><p>\u5fae\u4fe1\u516c\u4f17\u53f7\u662f\u4e00\u79cd\u57fa\u4e8e\u5fae\u4fe1\u5e73\u53f0\u7684\u516c\u4f17\u8d26\u53f7\uff0c\u53ef\u4ee5\u5411\u7528\u6237\u63a8\u9001\u6d88\u606f\u3001\u63d0\u4f9b\u670d\u52a1\u7b49\u3002\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u53f7\u8c03\u7528Python\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\uff0c\u6bd4\u5982\u81ea\u52a8\u56de\u590d\u6d88\u606f\u3001\u5904\u7406\u7528\u6237\u8bf7\u6c42\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6ce8\u518c\u5e76\u914d\u7f6e\u5fae\u4fe1\u516c\u4f17\u53f7<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u5fae\u4fe1\u516c\u4f17\u53f7\uff0c\u5e76\u8fdb\u884c\u57fa\u672c\u7684\u914d\u7f6e\u3002\u6ce8\u518c\u5fae\u4fe1\u516c\u4f17\u53f7\u53ef\u4ee5\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\uff08<a href=\"https:\/\/mp.weixin.qq.com%EF%BC%89%E8%BF%9B%E8%A1%8C%E3%80%82%E6%B3%A8%E5%86%8C%E5%AE%8C%E6%88%90%E5%90%8E%EF%BC%8C%E9%9C%80%E8%A6%81%E8%BF%9B%E8%A1%8C%E4%BB%A5%E4%B8%8B%E9%85%8D%E7%BD%AE%EF%BC%9A\">https:\/\/mp.weixin.qq.com\uff09\u8fdb\u884c\u3002\u6ce8\u518c\u5b8c\u6210\u540e\uff0c\u9700\u8981\u8fdb\u884c\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/a><\/p>\n<\/p>\n<ul>\n<li><strong>\u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740<\/strong>\uff1a\u5728\u5fae\u4fe1\u516c\u4f17\u53f7\u540e\u53f0\uff0c\u627e\u5230\u201c\u5f00\u53d1\u201d-&gt;\u201c\u57fa\u672c\u914d\u7f6e\u201d\uff0c\u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740\u3001Token\u7b49\u4fe1\u606f\u3002\u670d\u52a1\u5668\u5730\u5740\u662f\u4e00\u4e2a\u53ef\u4ee5\u8bbf\u95ee\u7684URL\uff0c\u7528\u4e8e\u63a5\u6536\u5fae\u4fe1\u670d\u52a1\u5668\u63a8\u9001\u7684\u6d88\u606f\u3002<\/li>\n<li><strong>\u83b7\u53d6AppID\u548cAppSecret<\/strong>\uff1a\u5728\u5fae\u4fe1\u516c\u4f17\u53f7\u540e\u53f0\uff0c\u627e\u5230\u201c\u5f00\u53d1\u201d-&gt;\u201c\u57fa\u672c\u914d\u7f6e\u201d\uff0c\u83b7\u53d6AppID\u548cAppSecret\uff0c\u7528\u4e8e\u540e\u7eed\u7684\u63a5\u53e3\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u642d\u5efaPython\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u642d\u5efa\u4e00\u4e2aPython\u670d\u52a1\u5668\uff0c\u7528\u4e8e\u63a5\u6536\u548c\u5904\u7406\u5fae\u4fe1\u516c\u4f17\u53f7\u7684\u8bf7\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528Flask\u3001Django\u7b49Web\u6846\u67b6\u6765\u642d\u5efa\u670d\u52a1\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Flask\u6846\u67b6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>import hashlib<\/p>\n<p>import xml.etree.ElementTree as ET<\/p>\n<p>app = Flask(__name__)<\/p>\n<h2><strong>\u914d\u7f6eToken<\/strong><\/h2>\n<p>TOKEN = &#39;your_token&#39;<\/p>\n<h2><strong>\u9a8c\u8bc1\u6d88\u606f\u6765\u81ea\u5fae\u4fe1\u670d\u52a1\u5668<\/strong><\/h2>\n<p>@app.route(&#39;\/wechat&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def wechat_verify():<\/p>\n<p>    signature = request.args.get(&#39;signature&#39;)<\/p>\n<p>    timestamp = request.args.get(&#39;timestamp&#39;)<\/p>\n<p>    nonce = request.args.get(&#39;nonce&#39;)<\/p>\n<p>    echostr = request.args.get(&#39;echostr&#39;)<\/p>\n<p>    # \u6309Token\u3001timestamp\u3001nonce\u5b57\u5178\u6392\u5e8f\u5e76\u8fde\u63a5\u6210\u5b57\u7b26\u4e32<\/p>\n<p>    data = &#39;&#39;.join(sorted([TOKEN, timestamp, nonce]))<\/p>\n<p>    # \u8ba1\u7b97sha1\u54c8\u5e0c\u503c<\/p>\n<p>    hashcode = hashlib.sha1(data.encode(&#39;utf-8&#39;)).hexdigest()<\/p>\n<p>    # \u6bd4\u8f83\u54c8\u5e0c\u503c<\/p>\n<p>    if hashcode == signature:<\/p>\n<p>        return echostr<\/p>\n<p>    else:<\/p>\n<p>        return &#39;Verification f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&#39;<\/p>\n<h2><strong>\u5904\u7406\u5fae\u4fe1\u6d88\u606f<\/strong><\/h2>\n<p>@app.route(&#39;\/wechat&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def wechat_handle():<\/p>\n<p>    xml_data = request.data<\/p>\n<p>    xml_tree = ET.fromstring(xml_data)<\/p>\n<p>    msg_type = xml_tree.find(&#39;MsgType&#39;).text<\/p>\n<p>    if msg_type == &#39;text&#39;:<\/p>\n<p>        content = xml_tree.find(&#39;Content&#39;).text<\/p>\n<p>        from_user = xml_tree.find(&#39;FromUserName&#39;).text<\/p>\n<p>        to_user = xml_tree.find(&#39;ToUserName&#39;).text<\/p>\n<p>        # \u5904\u7406\u6587\u672c\u6d88\u606f<\/p>\n<p>        reply_content = f&#39;You said: {content}&#39;<\/p>\n<p>        reply_xml = f&quot;&quot;&quot;<\/p>\n<p>        &lt;xml&gt;<\/p>\n<p>        &lt;ToUserName&gt;&lt;![CDATA[{from_user}]]&gt;&lt;\/ToUserName&gt;<\/p>\n<p>        &lt;FromUserName&gt;&lt;![CDATA[{to_user}]]&gt;&lt;\/FromUserName&gt;<\/p>\n<p>        &lt;CreateTime&gt;{int(time.time())}&lt;\/CreateTime&gt;<\/p>\n<p>        &lt;MsgType&gt;&lt;![CDATA[text]]&gt;&lt;\/MsgType&gt;<\/p>\n<p>        &lt;Content&gt;&lt;![CDATA[{reply_content}]]&gt;&lt;\/Content&gt;<\/p>\n<p>        &lt;\/xml&gt;<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        return reply_xml<\/p>\n<p>    else:<\/p>\n<p>        return &#39;success&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(port=5000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>wechat_verify<\/code>\u51fd\u6570\u7528\u4e8e\u9a8c\u8bc1\u6d88\u606f\u662f\u5426\u6765\u81ea\u5fae\u4fe1\u670d\u52a1\u5668\uff0c<code>wechat_handle<\/code>\u51fd\u6570\u7528\u4e8e\u5904\u7406\u5fae\u4fe1\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u90e8\u7f72Python\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u5c06\u642d\u5efa\u597d\u7684Python\u670d\u52a1\u5668\u90e8\u7f72\u5230\u4e00\u4e2a\u53ef\u4ee5\u8bbf\u95ee\u7684\u5730\u5740\u4e0a\uff0c\u6bd4\u5982\u4e91\u670d\u52a1\u5668\u3001\u865a\u62df\u4e3b\u673a\u7b49\u3002\u786e\u4fdd\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u516c\u7f51IP\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u914d\u7f6e\u5fae\u4fe1\u516c\u4f17\u53f7\u670d\u52a1\u5668\u5730\u5740<\/h4>\n<\/p>\n<p><p>\u5c06\u90e8\u7f72\u597d\u7684Python\u670d\u52a1\u5668\u5730\u5740\u914d\u7f6e\u5230\u5fae\u4fe1\u516c\u4f17\u53f7\u540e\u53f0\u7684\u201c\u5f00\u53d1\u201d-&gt;\u201c\u57fa\u672c\u914d\u7f6e\u201d\u4e2d\u7684\u201c\u670d\u52a1\u5668\u5730\u5740\uff08URL\uff09\u201d\u5b57\u6bb5\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>5\u3001\u6d4b\u8bd5\u5fae\u4fe1\u516c\u4f17\u53f7<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u5728\u5fae\u4fe1\u4e2d\u5173\u6ce8\u4f60\u7684\u5fae\u4fe1\u516c\u4f17\u53f7\uff0c\u5e76\u53d1\u9001\u6d88\u606f\u8fdb\u884c\u6d4b\u8bd5\u3002\u5982\u679c\u914d\u7f6e\u6b63\u786e\uff0cPython\u670d\u52a1\u5668\u4f1a\u63a5\u6536\u5230\u6d88\u606f\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u8fd4\u56de\u76f8\u5e94\u7684\u56de\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5fae\u4fe1\u5c0f\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u662f\u4e00\u79cd\u65e0\u9700\u4e0b\u8f7d\u5b89\u88c5\u5373\u53ef\u4f7f\u7528\u7684\u5e94\u7528\uff0c\u53ef\u4ee5\u5728\u5fae\u4fe1\u4e2d\u5feb\u901f\u6253\u5f00\u548c\u4f7f\u7528\u3002\u901a\u8fc7\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8c03\u7528Python\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u6570\u636e\u4ea4\u4e92\u3001\u540e\u53f0\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6ce8\u518c\u5e76\u914d\u7f6e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u5e76\u8fdb\u884c\u57fa\u672c\u7684\u914d\u7f6e\u3002\u6ce8\u518c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\uff08<a href=\"https:\/\/mp.weixin.qq.com%EF%BC%89%E8%BF%9B%E8%A1%8C%E3%80%82%E6%B3%A8%E5%86%8C%E5%AE%8C%E6%88%90%E5%90%8E%EF%BC%8C%E9%9C%80%E8%A6%81%E8%BF%9B%E8%A1%8C%E4%BB%A5%E4%B8%8B%E9%85%8D%E7%BD%AE%EF%BC%9A\">https:\/\/mp.weixin.qq.com\uff09\u8fdb\u884c\u3002\u6ce8\u518c\u5b8c\u6210\u540e\uff0c\u9700\u8981\u8fdb\u884c\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/a><\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6AppID\u548cAppSecret<\/strong>\uff1a\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u540e\u53f0\uff0c\u627e\u5230\u201c\u5f00\u53d1\u201d-&gt;\u201c\u57fa\u672c\u914d\u7f6e\u201d\uff0c\u83b7\u53d6AppID\u548cAppSecret\uff0c\u7528\u4e8e\u540e\u7eed\u7684\u63a5\u53e3\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u642d\u5efaPython\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u4e0e\u5fae\u4fe1\u516c\u4f17\u53f7\u7c7b\u4f3c\uff0c\u9700\u8981\u642d\u5efa\u4e00\u4e2aPython\u670d\u52a1\u5668\uff0c\u7528\u4e8e\u63a5\u6536\u548c\u5904\u7406\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u8bf7\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528Flask\u3001Django\u7b49Web\u6846\u67b6\u6765\u642d\u5efa\u670d\u52a1\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Flask\u6846\u67b6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<h2><strong>\u83b7\u53d6\u5c0f\u7a0b\u5e8f\u767b\u5f55\u51ed\u8bc1<\/strong><\/h2>\n<p>@app.route(&#39;\/login&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def login():<\/p>\n<p>    code = request.json.get(&#39;code&#39;)<\/p>\n<p>    # \u4f7f\u7528code\u6362\u53d6session_key\u548copenid<\/p>\n<p>    response = requests.get(f&#39;https:\/\/api.weixin.qq.com\/sns\/jscode2session?appid=your_appid&amp;secret=your_appsecret&amp;js_code={code}&amp;grant_type=authorization_code&#39;)<\/p>\n<p>    data = response.json()<\/p>\n<p>    session_key = data.get(&#39;session_key&#39;)<\/p>\n<p>    openid = data.get(&#39;openid&#39;)<\/p>\n<p>    return jsonify({&#39;session_key&#39;: session_key, &#39;openid&#39;: openid})<\/p>\n<h2><strong>\u5904\u7406\u5176\u4ed6\u8bf7\u6c42<\/strong><\/h2>\n<p>@app.route(&#39;\/process&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def process():<\/p>\n<p>    # \u5904\u7406\u5c0f\u7a0b\u5e8f\u8bf7\u6c42<\/p>\n<p>    data = request.json<\/p>\n<p>    result = some_python_function(data)<\/p>\n<p>    return jsonify({&#39;result&#39;: result})<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(port=5000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>login<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5c0f\u7a0b\u5e8f\u767b\u5f55\u51ed\u8bc1\uff0c<code>process<\/code>\u51fd\u6570\u7528\u4e8e\u5904\u7406\u5c0f\u7a0b\u5e8f\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u90e8\u7f72Python\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u5c06\u642d\u5efa\u597d\u7684Python\u670d\u52a1\u5668\u90e8\u7f72\u5230\u4e00\u4e2a\u53ef\u4ee5\u8bbf\u95ee\u7684\u5730\u5740\u4e0a\uff0c\u6bd4\u5982\u4e91\u670d\u52a1\u5668\u3001\u865a\u62df\u4e3b\u673a\u7b49\u3002\u786e\u4fdd\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u516c\u7f51IP\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u524d\u7aef<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u524d\u7aef\uff0c\u7f16\u5199\u5c0f\u7a0b\u5e8f\u4ee3\u7801\uff0c\u901a\u8fc7HTTP\u8bf7\u6c42\u8c03\u7528Python\u670d\u52a1\u5668\u7684\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u7a0b\u5e8f\u524d\u7aef\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;pages&quot;: [<\/p>\n<p>    &quot;pages\/index\/index&quot;<\/p>\n<p>  ]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;navigationBarTitleText&quot;: &quot;\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8c03\u7528Python\u7a0b\u5e8f&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;view class=&quot;container&quot;&gt;<\/p>\n<p>  &lt;button bindtap=&quot;login&quot;&gt;\u767b\u5f55&lt;\/button&gt;<\/p>\n<p>  &lt;button bindtap=&quot;process&quot;&gt;\u5904\u7406\u8bf7\u6c42&lt;\/button&gt;<\/p>\n<p>&lt;\/view&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-js\">Page({<\/p>\n<p>  data: {},<\/p>\n<p>  login: function() {<\/p>\n<p>    wx.login({<\/p>\n<p>      success: function(res) {<\/p>\n<p>        if (res.code) {<\/p>\n<p>          wx.request({<\/p>\n<p>            url: &#39;https:\/\/your_server_address\/login&#39;,<\/p>\n<p>            method: &#39;POST&#39;,<\/p>\n<p>            data: { code: res.code },<\/p>\n<p>            success: function(response) {<\/p>\n<p>              console.log(response.data);<\/p>\n<p>            }<\/p>\n<p>          });<\/p>\n<p>        }<\/p>\n<p>      }<\/p>\n<p>    });<\/p>\n<p>  },<\/p>\n<p>  process: function() {<\/p>\n<p>    wx.request({<\/p>\n<p>      url: &#39;https:\/\/your_server_address\/process&#39;,<\/p>\n<p>      method: &#39;POST&#39;,<\/p>\n<p>      data: { key: &#39;value&#39; },<\/p>\n<p>      success: function(response) {<\/p>\n<p>        console.log(response.data);<\/p>\n<p>      }<\/p>\n<p>    });<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>login<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u7528Python\u670d\u52a1\u5668\u7684\u767b\u5f55\u63a5\u53e3\uff0c<code>process<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u7528Python\u670d\u52a1\u5668\u7684\u5904\u7406\u8bf7\u6c42\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>5\u3001\u6d4b\u8bd5\u5fae\u4fe1\u5c0f\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u5728\u5fae\u4fe1\u5f00\u53d1\u8005\u5de5\u5177\u4e2d\u8fd0\u884c\u5e76\u6d4b\u8bd5\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u3002\u5982\u679c\u914d\u7f6e\u6b63\u786e\uff0c\u5c0f\u7a0b\u5e8f\u524d\u7aef\u4f1a\u901a\u8fc7HTTP\u8bf7\u6c42\u8c03\u7528Python\u670d\u52a1\u5668\u7684\u63a5\u53e3\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7b2c\u4e09\u65b9\u5f00\u53d1\u5e73\u53f0<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5fae\u4fe1\u516c\u4f17\u53f7\u548c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5f00\u53d1\u5e73\u53f0\uff0c\u6bd4\u5982WeChaty\u3001\u5fae\u4fe1\u5f00\u653e\u5e73\u53f0\u7b49\uff0c\u901a\u8fc7\u8fd9\u4e9b\u5e73\u53f0\u8c03\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001WeChaty<\/h4>\n<\/p>\n<p><p>WeChaty\u662f\u4e00\u4e2a\u5fae\u4fe1\u804a\u5929\u673a\u5668\u4eba\u6846\u67b6\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7f16\u5199\u804a\u5929\u673a\u5668\u4eba\uff0c\u5b9e\u73b0\u81ea\u52a8\u56de\u590d\u3001\u6d88\u606f\u5904\u7406\u7b49\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528WeChaty\u6846\u67b6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wechaty import Wechaty, Contact, Message<\/p>\n<p>class MyBot(Wechaty):<\/p>\n<p>    async def on_message(self, msg: Message):<\/p>\n<p>        from_contact = msg.talker()<\/p>\n<p>        text = msg.text()<\/p>\n<p>        if text == &#39;hello&#39;:<\/p>\n<p>            await from_contact.say(&#39;Hello! How can I help you?&#39;)<\/p>\n<p>        else:<\/p>\n<p>            await from_contact.say(f&#39;You said: {text}&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    bot = MyBot()<\/p>\n<p>    bot.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>MyBot<\/code>\u7c7b\u7ee7\u627f\u81ea<code>Wechaty<\/code>\uff0c\u5b9e\u73b0\u4e86<code>on_message<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u5904\u7406\u6536\u5230\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5fae\u4fe1\u5f00\u653e\u5e73\u53f0<\/h4>\n<\/p>\n<p><p>\u5fae\u4fe1\u5f00\u653e\u5e73\u53f0\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217API\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u8c03\u7528\u8fd9\u4e9bAPI\uff0c\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\uff0c\u6bd4\u5982\u6d88\u606f\u53d1\u9001\u3001\u7528\u6237\u7ba1\u7406\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5fae\u4fe1\u5f00\u653e\u5e73\u53f0API\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u83b7\u53d6Access Token<\/strong><\/h2>\n<p>def get_access_token(appid, appsecret):<\/p>\n<p>    response = requests.get(f&#39;https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid={appid}&amp;secret={appsecret}&#39;)<\/p>\n<p>    data = response.json()<\/p>\n<p>    access_token = data.get(&#39;access_token&#39;)<\/p>\n<p>    return access_token<\/p>\n<h2><strong>\u53d1\u9001\u6d88\u606f<\/strong><\/h2>\n<p>def send_message(access_token, openid, content):<\/p>\n<p>    url = f&#39;https:\/\/api.weixin.qq.com\/cgi-bin\/message\/custom\/send?access_token={access_token}&#39;<\/p>\n<p>    data = {<\/p>\n<p>        &#39;touser&#39;: openid,<\/p>\n<p>        &#39;msgtype&#39;: &#39;text&#39;,<\/p>\n<p>        &#39;text&#39;: {<\/p>\n<p>            &#39;content&#39;: content<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    response = requests.post(url, json=data)<\/p>\n<p>    return response.json()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    appid = &#39;your_appid&#39;<\/p>\n<p>    appsecret = &#39;your_appsecret&#39;<\/p>\n<p>    openid = &#39;user_openid&#39;<\/p>\n<p>    content = &#39;Hello, this is a message from Python program!&#39;<\/p>\n<p>    access_token = get_access_token(appid, appsecret)<\/p>\n<p>    result = send_message(access_token, openid, content)<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>get_access_token<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6Access Token\uff0c<code>send_message<\/code>\u51fd\u6570\u7528\u4e8e\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Web\u63a5\u53e3<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7Web\u63a5\u53e3\u8c03\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u6bd4\u5982RESTful API\u3001GraphQL\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Flask\u642d\u5efaRESTful API\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/api&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def api():<\/p>\n<p>    data = request.json<\/p>\n<p>    result = some_python_function(data)<\/p>\n<p>    return jsonify({&#39;result&#39;: result})<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(port=5000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>api<\/code>\u51fd\u6570\u7528\u4e8e\u5904\u7406POST\u8bf7\u6c42\uff0c\u5e76\u8fd4\u56de\u5904\u7406\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u53f7\u3001\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u3001\u7b2c\u4e09\u65b9\u5f00\u53d1\u5e73\u53f0\u3001\u4f7f\u7528Web\u63a5\u53e3\u7b49\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u5fae\u4fe1\u4e2d\u8c03\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u8fdb\u884c\u5f00\u53d1\u548c\u5b9e\u73b0\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u5fae\u4fe1\u4e2d\u8c03\u7528Python\u7a0b\u5e8f\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u5fae\u4fe1\u4e2d\u4f7f\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u5728\u5fae\u4fe1\u4e2d\u4f7f\u7528Python\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\u6216\u5c0f\u7a0b\u5e8f\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0\uff0c\u60a8\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\uff0c\u8fd9\u4e2a\u670d\u52a1\u4f7f\u7528Python\u7f16\u5199\uff0c\u5904\u7406\u7528\u6237\u8bf7\u6c42\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49\u6846\u67b6\u6765\u5f00\u53d1API\uff0c\u5e76\u901a\u8fc7\u5fae\u4fe1\u7684\u63a5\u53e3\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<p><strong>\u662f\u5426\u9700\u8981\u6ce8\u518c\u5fae\u4fe1\u516c\u4f17\u8d26\u53f7\u624d\u80fd\u8c03\u7528Python\u7a0b\u5e8f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\u5fae\u4fe1\u516c\u4f17\u8d26\u53f7\u6216\u5c0f\u7a0b\u5e8f\u8d26\u53f7\uff0c\u8fd9\u6837\u60a8\u624d\u80fd\u8bbf\u95ee\u5fae\u4fe1\u7684API\u63a5\u53e3\u3002\u6ce8\u518c\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u516c\u4f17\u5e73\u53f0\u4e0a\u914d\u7f6e\u670d\u52a1\u5668\u8bbe\u7f6e\uff0c\u5c06\u60a8\u7684Python\u7a0b\u5e8f\u90e8\u7f72\u5230\u670d\u52a1\u5668\uff0c\u5e76\u901a\u8fc7URL\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fddPython\u7a0b\u5e8f\u4e0e\u5fae\u4fe1\u7684\u4ea4\u4e92\u5b89\u5168\uff1f<\/strong><br \/>\u786e\u4fdd\u5b89\u5168\u7684\u4ea4\u4e92\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528HTTPS\u52a0\u5bc6\u901a\u4fe1\uff0c\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5229\u7528\u5fae\u4fe1\u63d0\u4f9b\u7684\u7b7e\u540d\u9a8c\u8bc1\u673a\u5236\uff0c\u786e\u4fdd\u8bf7\u6c42\u7684\u6765\u6e90\u5408\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u4f2a\u9020\u8bf7\u6c42\u548c\u6570\u636e\u6cc4\u9732\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5fae\u4fe1\u5982\u4f55\u8c03\u7528Python\u7f16\u7684\u7a0b\u5e8f \u5728\u5fae\u4fe1\u4e2d\u8c03\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5fae\u4fe1\u516c\u4f17\u53f7\u3001\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u3001\u7b2c\u4e09\u65b9\u5f00 [&hellip;]","protected":false},"author":3,"featured_media":1026219,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026215"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1026215"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026215\/revisions"}],"predecessor-version":[{"id":1026221,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026215\/revisions\/1026221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026219"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}