{"id":957705,"date":"2024-12-27T03:06:48","date_gmt":"2024-12-26T19:06:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/957705.html"},"modified":"2024-12-27T03:06:50","modified_gmt":"2024-12-26T19:06:50","slug":"web%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8cpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/957705.html","title":{"rendered":"web\u4e2d\u5982\u4f55\u8fd0\u884cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100932\/285d93bd-aef9-4583-a03b-dd11340c12ef.webp\" alt=\"web\u4e2d\u5982\u4f55\u8fd0\u884cpython\" \/><\/p>\n<p><p> <strong>\u5728Web\u4e2d\u8fd0\u884cPython\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Web\u6846\u67b6\u3001\u5d4c\u5165Python\u4ee3\u7801\u3001\u4f7f\u7528Web\u670d\u52a1\u5668\u7f51\u5173\u63a5\u53e3\uff08WSGI\uff09\u3001\u901a\u8fc7API\u8c03\u7528Python\u811a\u672c\u3002<\/strong>\u9996\u5148\uff0c\u4f7f\u7528Web\u6846\u67b6\u5982Flask\u548cDjango\uff0c\u53ef\u4ee5\u7b80\u5316Python\u5728Web\u5e94\u7528\u4e2d\u7684\u96c6\u6210\u8fc7\u7a0b\u3002\u5176\u6b21\uff0c\u5d4c\u5165Python\u4ee3\u7801\u5230HTML\u4e2d\uff0c\u901a\u8fc7\u6a21\u677f\u5f15\u64ce\u5982Jinja2\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u751f\u6210\u3002\u6700\u540e\uff0c\u4f7f\u7528WSGI\u6807\u51c6\uff0c\u53ef\u4ee5\u786e\u4fddPython\u5e94\u7528\u4e0eWeb\u670d\u52a1\u5668\u9ad8\u6548\u901a\u4fe1\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Web\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Web\u6846\u67b6\u662f\u8fd0\u884cPython\u4ee3\u7801\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u4e4b\u4e00\u3002Web\u6846\u67b6\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u642d\u5efaWeb\u5e94\u7528\uff0c\u5904\u7406HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u5e76\u63d0\u4f9b\u591a\u79cd\u6269\u5c55\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Flask<\/h4>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u5c0f\u578b\u5e94\u7528\u548c\u5fae\u670d\u52a1\u3002\u5b83\u6613\u4e8e\u4e0a\u624b\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6269\u5c55\u4ee5\u652f\u6301\u6570\u636e\u5e93\u3001\u8868\u5355\u3001\u8eab\u4efd\u9a8c\u8bc1\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Flask\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello_world():<\/p>\n<p>    return &#39;Hello, World!&#39;<\/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(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8be5\u811a\u672c\u540e\uff0c\u8bbf\u95ee<code>http:\/\/localhost:5000<\/code>\u5373\u53ef\u770b\u5230\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2\u3001Django<\/h4>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5927\u578bWeb\u5e94\u7528\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86ORM\u3001\u6a21\u677f\u5f15\u64ce\u3001\u7528\u6237\u8ba4\u8bc1\u7b49\u4f17\u591a\u5185\u7f6e\u529f\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5b89\u88c5Django\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject mysite<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Django\u9879\u76ee\u7ed3\u6784\u8f83\u4e3a\u590d\u6742\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5d4c\u5165Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728HTML\u4e2d\u5d4c\u5165Python\u4ee3\u7801\u4ee5\u751f\u6210\u52a8\u6001\u5185\u5bb9\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6a21\u677f\u5f15\u64ce\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u6a21\u677f\u5f15\u64ce<\/h4>\n<\/p>\n<p><p>\u6a21\u677f\u5f15\u64ce\u5982Jinja2\u5141\u8bb8\u5f00\u53d1\u8005\u5728HTML\u4e2d\u5d4c\u5165Python\u4ee3\u7801\uff0c\u4ee5\u52a8\u6001\u751f\u6210\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>Jinja2\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>Jinja2\u53ef\u4ee5\u4e0eFlask\u96c6\u6210\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    return render_template(&#39;index.html&#39;, name=&#39;World&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>index.html<\/code>\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Hello, {{ name }}!&lt;\/h1&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Jinja2\u4f1a\u5c06<code>{{ name }}<\/code>\u66ff\u6362\u4e3a\u4f20\u9012\u7684\u53c2\u6570\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528WSGI<\/h3>\n<\/p>\n<p><p>WSGI\u662fWeb\u670d\u52a1\u5668\u548cPython\u5e94\u7528\u4e4b\u95f4\u7684\u901a\u4fe1\u534f\u8bae\u3002\u5b83\u662fPython Web\u5e94\u7528\u7684\u6807\u51c6\uff0c\u4f7f\u5f97Web\u670d\u52a1\u5668\u548c\u5e94\u7528\u4e4b\u95f4\u7684\u4ea4\u4e92\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1\u3001WSGI\u5de5\u4f5c\u539f\u7406<\/h4>\n<\/p>\n<p><p>WSGI\u5206\u4e3a\u4e24\u90e8\u5206\uff1a\u670d\u52a1\u5668\uff08\u5904\u7406HTTP\u8bf7\u6c42\uff09\u548c\u5e94\u7528\uff08\u5904\u7406\u4e1a\u52a1\u903b\u8f91\uff09\u3002\u5e94\u7528\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff08\u5982\u51fd\u6570\uff09\u6765\u5904\u7406\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7b80\u5355\u7684WSGI\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684WSGI\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def simple_app(environ, start_response):<\/p>\n<p>    status = &#39;200 OK&#39;<\/p>\n<p>    headers = [(&#39;Content-type&#39;, &#39;text\/plain; charset=utf-8&#39;)]<\/p>\n<p>    start_response(status, headers)<\/p>\n<p>    return [b&quot;Hello World&quot;]<\/p>\n<p>from wsgiref.simple_server import make_server<\/p>\n<p>httpd = make_server(&#39;&#39;, 8000, simple_app)<\/p>\n<p>print(&quot;Serving on port 8000...&quot;)<\/p>\n<p>httpd.serve_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528WSGI\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7API\u8c03\u7528Python\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7API\u8c03\u7528Python\u811a\u672c\uff0c\u7279\u522b\u662f\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Flask\u521b\u5efaAPI<\/h4>\n<\/p>\n<p><p>Flask\u53ef\u4ee5\u8f7b\u677e\u521b\u5efaRESTful API\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5e94\u7528\u6216\u670d\u52a1\u8c03\u7528Python\u811a\u672c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u7b80\u5355API<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/api\/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;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbf\u95ee<code>\/api\/data<\/code>\u4f1a\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2\u3001\u4f7f\u7528FastAPI<\/h4>\n<\/p>\n<p><p>FastAPI\u662f\u4e00\u4e2a\u73b0\u4ee3\u7684\u3001\u5feb\u901f\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u521b\u5efa\u9ad8\u6027\u80fd\u7684API\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5b89\u88c5FastAPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install fastapi uvicorn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684FastAPI\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fastapi import FastAPI<\/p>\n<p>app = FastAPI()<\/p>\n<p>@app.get(&quot;\/&quot;)<\/p>\n<p>async def read_root():<\/p>\n<p>    return {&quot;Hello&quot;: &quot;World&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u542f\u52a8\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">uvicorn main:app --reload<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>FastAPI\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\uff0c\u80fd\u591f\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Web\u4e2d\u8fd0\u884cPython\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u6808\u3002<strong>\u4f7f\u7528Web\u6846\u67b6\u5982Flask\u548cDjango\u3001\u5d4c\u5165Python\u4ee3\u7801\u3001\u4f7f\u7528WSGI\u6807\u51c6\u3001\u901a\u8fc7API\u8c03\u7528Python\u811a\u672c<\/strong>\uff0c\u8fd9\u4e9b\u90fd\u662f\u6709\u6548\u7684\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u6784\u5efa\u51fa\u529f\u80fd\u5f3a\u5927\u3001\u9ad8\u6548\u7684Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u7f51\u9875\u4e0a\u8fd0\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u5728\u7f51\u9875\u4e0a\u8fd0\u884cPython\u4ee3\u7801\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5728\u7ebf\u7f16\u8bd1\u5668\u6216\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u7f51\u7ad9\u5982Replit\u3001Glitch\u548cPythonAnywhere\u90fd\u5141\u8bb8\u7528\u6237\u7f16\u5199\u548c\u6267\u884cPython\u4ee3\u7801\u3002\u7528\u6237\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u8d26\u6237\uff0c\u9009\u62e9Python\u4f5c\u4e3a\u7f16\u7a0b\u8bed\u8a00\uff0c\u5373\u53ef\u5f00\u59cb\u7f16\u5199\u548c\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<p><strong>Python\u4e0eWeb\u5f00\u53d1\u7684\u5e38\u89c1\u6846\u67b6\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Web\u5f00\u53d1\u4e2d\uff0cPython\u6709\u51e0\u4e2a\u6d41\u884c\u7684\u6846\u67b6\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\u3002Flask\u548cDjango\u662f\u4e24\u4e2a\u6700\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u3002Flask\u9002\u5408\u5c0f\u578b\u9879\u76ee\uff0c\u7075\u6d3b\u6027\u9ad8\uff0c\u800cDjango\u5219\u63d0\u4f9b\u4e86\u66f4\u5168\u9762\u7684\u529f\u80fd\uff0c\u9002\u5408\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u4e0eHTML\u7ed3\u5408\u4f7f\u7528\uff1f<\/strong><br \/>\u5c06Python\u4e0eHTML\u7ed3\u5408\u4f7f\u7528\u901a\u5e38\u6d89\u53ca\u5230\u670d\u52a1\u5668\u7aef\u7f16\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528Flask\u6216Django\u7b49\u6846\u67b6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u521b\u5efa\u52a8\u6001\u7f51\u9875\u3002Python\u4ee3\u7801\u53ef\u4ee5\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u5e76\u6700\u7ec8\u751f\u6210HTML\u9875\u9762\u4ee5\u5c55\u793a\u7ed9\u7528\u6237\u3002\u7528\u6237\u53ef\u4ee5\u5b66\u4e60\u5982\u4f55\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u6e32\u67d3\u6a21\u677f\u6765\u5b9e\u73b0\u8fd9\u79cd\u7ed3\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Web\u4e2d\u8fd0\u884cPython\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Web\u6846\u67b6\u3001\u5d4c\u5165Python\u4ee3\u7801\u3001\u4f7f\u7528Web\u670d\u52a1\u5668\u7f51\u5173\u63a5\u53e3\uff08WS [&hellip;]","protected":false},"author":3,"featured_media":957711,"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\/957705"}],"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=957705"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/957705\/revisions"}],"predecessor-version":[{"id":957712,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/957705\/revisions\/957712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/957711"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=957705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=957705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=957705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}