{"id":1014694,"date":"2024-12-27T11:58:34","date_gmt":"2024-12-27T03:58:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1014694.html"},"modified":"2024-12-27T11:58:36","modified_gmt":"2024-12-27T03:58:36","slug":"axure%e5%92%8cpython%e5%a6%82%e4%bd%95%e8%bf%9e%e6%8e%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1014694.html","title":{"rendered":"Axure\u548cpython\u5982\u4f55\u8fde\u63a5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094733\/0a36441f-b481-4178-84ab-6484091467f3.webp\" alt=\"Axure\u548cpython\u5982\u4f55\u8fde\u63a5\" \/><\/p>\n<p><p> <strong>Axure\u548cPython\u53ef\u4ee5\u901a\u8fc7API\u3001\u6587\u4ef6\u4ea4\u6362\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u65b9\u5f0f\u8fde\u63a5\u3002<\/strong>\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u63a2\u8ba8\u901a\u8fc7API\u7684\u65b9\u5f0f\u8fdb\u884c\u8fde\u63a5\u3002API\uff08\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff09\u662f\u4e00\u4e2a\u8f6f\u4ef6\u4e2d\u4ecb\uff0c\u5b83\u5141\u8bb8\u4e24\u4e2a\u5e94\u7528\u7a0b\u5e8f\u76f8\u4e92\u901a\u4fe1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u521b\u5efa\u4e00\u4e2aAPI\u7aef\u70b9\uff0cAxure\u53ef\u4ee5\u901a\u8fc7HTTP\u8bf7\u6c42\u6765\u8bbf\u95ee\u8fd9\u4e2a\u7aef\u70b9\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u4ea4\u6362\u548c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001API\u8fde\u63a5\u65b9\u5f0f<\/strong><\/h2>\n<p><p>API\u8fde\u63a5\u662f\u73b0\u4ee3\u8f6f\u4ef6\u96c6\u6210\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u5f0f\u3002Python\u53ef\u4ee5\u4f7f\u7528\u8bb8\u591a\u6846\u67b6\u6765\u521b\u5efaAPI\uff0c\u4f8b\u5982Flask\u548cDjango\u3002\u901a\u8fc7\u8fd9\u4e9b\u6846\u67b6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u63a5\u6536\u548c\u54cd\u5e94HTTP\u8bf7\u6c42\u7684\u670d\u52a1\u5668\u3002\u5728Axure\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528JavaScript\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u4ece\u800c\u4e0ePython API\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528Flask\u521b\u5efaAPI<\/h2>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684API\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Flask\u521b\u5efaAPI\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, jsonify, request<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/data&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def get_data():<\/p>\n<p>    data = {&#39;key&#39;: &#39;value&#39;}<\/p>\n<p>    return jsonify(data)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(port=5000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684API\u7aef\u70b9<code>\/data<\/code>\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2aJSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u5728\u8fd9\u91cc\u8fd4\u56de\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h2>2. \u5728Axure\u4e2d\u53d1\u9001HTTP\u8bf7\u6c42<\/h2>\n<\/p>\n<p><p>Axure\u539f\u578b\u4e2d\u53ef\u4ee5\u5d4c\u5165JavaScript\u4ee3\u7801\uff0c\u901a\u8fc7JavaScript\u53ef\u4ee5\u5b9e\u73b0\u4e0eAPI\u7684\u901a\u4fe1\u3002\u8fd9\u91cc\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4eceAxure\u4e2d\u5411Flask API\u53d1\u9001GET\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var xhr = new XMLHttpRequest();<\/p>\n<p>xhr.open(&quot;GET&quot;, &quot;http:\/\/localhost:5000\/data&quot;, true);<\/p>\n<p>xhr.onreadystatechange = function () {<\/p>\n<p>    if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {<\/p>\n<p>        var response = JSON.parse(xhr.responseText);<\/p>\n<p>        console.log(response);<\/p>\n<p>        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e<\/p>\n<p>    }<\/p>\n<p>};<\/p>\n<p>xhr.send();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aXMLHttpRequest\u5bf9\u8c61\uff0c\u5e76\u5411Flask\u670d\u52a1\u5668\u53d1\u9001\u4e86GET\u8bf7\u6c42\u3002\u63a5\u6536\u5230\u54cd\u5e94\u540e\uff0c\u53ef\u4ee5\u5904\u7406\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u6587\u4ef6\u4ea4\u6362\u65b9\u5f0f<\/strong><\/h2>\n<p><p>\u6587\u4ef6\u4ea4\u6362\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684Axure\u548cPython\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u65b9\u5f0f\u3002Python\u53ef\u4ee5\u751f\u6210\u6570\u636e\u6587\u4ef6\uff08\u5982JSON\u6216CSV\uff09\uff0cAxure\u53ef\u4ee5\u901a\u8fc7JavaScript\u8bfb\u53d6\u8fd9\u4e9b\u6587\u4ef6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528Python\u751f\u6210\u6570\u636e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u751f\u6210JSON\u6587\u4ef6\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;key&#39;: &#39;value&#39;}<\/p>\n<p>with open(&#39;data.json&#39;, &#39;w&#39;) as json_file:<\/p>\n<p>    json.dump(data, json_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aPython\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>data.json<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u5199\u5165\u5176\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u5728Axure\u4e2d\u8bfb\u53d6\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>Axure\u53ef\u4ee5\u901a\u8fc7JavaScript\u8bfb\u53d6\u672c\u5730\u6587\u4ef6\u7684\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u501f\u52a9\u6d4f\u89c8\u5668\u7684\u6587\u4ef6\u8bfb\u53d6API\u6216\u8005\u5f15\u5165\u5916\u90e8\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">fetch(&#39;data.json&#39;)<\/p>\n<p>    .then(response =&gt; response.json())<\/p>\n<p>    .then(data =&gt; {<\/p>\n<p>        console.log(data);<\/p>\n<p>        \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u8bfb\u53d6\u5230\u7684\u6570\u636e<\/p>\n<p>    });<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aJavaScript\u4ee3\u7801\u4f7f\u7528Fetch API\u4ece\u672c\u5730\u8bfb\u53d6<code>data.json<\/code>\u6587\u4ef6\uff0c\u5e76\u89e3\u6790\u4e3aJSON\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u7f51\u7edc\u8bf7\u6c42\u65b9\u5f0f<\/strong><\/h2>\n<p><p>\u9664\u4e86API\u548c\u6587\u4ef6\u4ea4\u6362\uff0cAxure\u548cPython\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u7f51\u7edc\u8bf7\u6c42\u65b9\u5f0f\u8fdb\u884c\u4ea4\u4e92\uff0c\u4f8b\u5982WebSocket\u3002WebSocket\u662f\u4e00\u4e2a\u5168\u53cc\u5de5\u901a\u4fe1\u534f\u8bae\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528Python\u5b9e\u73b0WebSocket\u670d\u52a1\u5668<\/h2>\n<\/p>\n<p><p>Python\u53ef\u4ee5\u4f7f\u7528<code>websocket<\/code>\u5e93\u6765\u521b\u5efaWebSocket\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import websockets<\/p>\n<p>async def echo(websocket, path):<\/p>\n<p>    async for message in websocket:<\/p>\n<p>        await websocket.send(f&quot;Echo: {message}&quot;)<\/p>\n<p>start_server = websockets.serve(echo, &quot;localhost&quot;, 6789)<\/p>\n<p>asyncio.get_event_loop().run_until_complete(start_server)<\/p>\n<p>asyncio.get_event_loop().run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684WebSocket\u670d\u52a1\u5668\uff0c\u63a5\u6536\u5230\u6d88\u606f\u540e\u4f1a\u539f\u6837\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h2>2. \u5728Axure\u4e2d\u4f7f\u7528WebSocket<\/h2>\n<\/p>\n<p><p>Axure\u4e2d\u53ef\u4ee5\u4f7f\u7528JavaScript\u521b\u5efaWebSocket\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">var socket = new WebSocket(&#39;ws:\/\/localhost:6789&#39;);<\/p>\n<p>socket.onopen = function() {<\/p>\n<p>    console.log(&#39;WebSocket connection opened&#39;);<\/p>\n<p>    socket.send(&#39;Hello, WebSocket&#39;);<\/p>\n<p>};<\/p>\n<p>socket.onmessage = function(event) {<\/p>\n<p>    console.log(&#39;Received:&#39;, event.data);<\/p>\n<p>    \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aJavaScript\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aWebSocket\u8fde\u63a5\uff0c\u5e76\u53d1\u9001\u4e86\u4e00\u6761\u6d88\u606f\u3002\u6536\u5230\u6d88\u606f\u540e\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0cAxure\u548cPython\u53ef\u4ee5\u5b9e\u73b0\u6709\u6548\u7684\u6570\u636e\u4ea4\u6362\u548c\u4ea4\u4e92\u3002<strong>API\u8fde\u63a5\u3001\u6587\u4ef6\u4ea4\u6362\u3001\u7f51\u7edc\u8bf7\u6c42<\/strong>\u7b49\u65b9\u5f0f\u5404\u6709\u4f18\u52a3\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002API\u8fde\u63a5\u9002\u5408\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\u7684\u573a\u666f\uff0c\u6587\u4ef6\u4ea4\u6362\u9002\u5408\u79bb\u7ebf\u6570\u636e\u4f20\u8f93\uff0cWebSocket\u9002\u5408\u9700\u8981\u6301\u7eed\u901a\u4fe1\u7684\u5e94\u7528\u573a\u666f\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u8003\u8651\u5230\u5b89\u5168\u6027\u548c\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u548c\u53ef\u9760\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Axure\u4e2d\u4f7f\u7528Python\u4ee3\u7801\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\uff1f<\/strong><br \/>Axure\u4e3b\u8981\u7528\u4e8e\u539f\u578b\u8bbe\u8ba1\uff0c\u800cPython\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u8981\u5728Axure\u4e2d\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\uff0c\u53ef\u4ee5\u901a\u8fc7API\u8c03\u7528\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u9700\u8981\u5728Python\u4e2d\u642d\u5efa\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u5904\u7406Axure\u53d1\u9001\u7684\u8bf7\u6c42\u3002\u7136\u540e\uff0c\u5728Axure\u4e2d\u4f7f\u7528\u201c\u94fe\u63a5\u201d\u6216\u201c\u4e8b\u4ef6\u201d\u529f\u80fd\uff0c\u53d1\u9001HTTP\u8bf7\u6c42\u5230Python\u670d\u52a1\u5668\uff0c\u83b7\u53d6\u5e76\u5c55\u793a\u76f8\u5e94\u7684\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u548c\u52a8\u6001\u6570\u636e\u5c55\u793a\u3002<\/p>\n<p><strong>Axure\u4e0ePython\u8fde\u63a5\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u6216\u5e93\uff1f<\/strong><br \/>\u4e3a\u4e86\u5b9e\u73b0Axure\u4e0ePython\u7684\u8fde\u63a5\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528Flask\u6216Django\u7b49Python\u6846\u67b6\u6765\u521b\u5efaAPI\u3002\u8fd9\u4e9b\u6846\u67b6\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\uff0c\u5904\u7406\u6765\u81eaAxure\u7684\u8bf7\u6c42\u3002\u540c\u65f6\uff0c\u4f7f\u7528Postman\u7b49\u5de5\u5177\u8fdb\u884c\u63a5\u53e3\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u80fd\u591f\u987a\u5229\u4f20\u8f93\u5e76\u6b63\u786e\u8fd4\u56de\u3002<\/p>\n<p><strong>\u80fd\u5426\u5728Axure\u4e2d\u5b9e\u65f6\u5c55\u793aPython\u5904\u7406\u7684\u6570\u636e\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5728Axure\u4e2d\u5b9e\u65f6\u5c55\u793aPython\u5904\u7406\u7684\u6570\u636e\u3002\u901a\u8fc7\u8bbe\u7f6eAxure\u7684\u52a8\u6001\u9762\u677f\u548c\u53d8\u91cf\uff0c\u7ed3\u5408Python\u63d0\u4f9b\u7684API\u63a5\u53e3\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u5728\u4f7f\u7528\u539f\u578b\u65f6\u770b\u5230\u5b9e\u65f6\u66f4\u65b0\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u5728Axure\u4e2d\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7AJAX\u8bf7\u6c42\u5c06\u6570\u636e\u53d1\u9001\u5230Python\u540e\u7aef\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u7ed3\u679c\u66f4\u65b0\u9875\u9762\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u5927\u5e45\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Axure\u548cPython\u53ef\u4ee5\u901a\u8fc7API\u3001\u6587\u4ef6\u4ea4\u6362\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u65b9\u5f0f\u8fde\u63a5\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u63a2\u8ba8\u901a\u8fc7API\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1014700,"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\/1014694"}],"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=1014694"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014694\/revisions"}],"predecessor-version":[{"id":1014702,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014694\/revisions\/1014702"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1014700"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1014694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1014694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1014694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}