{"id":204853,"date":"2025-05-29T12:17:06","date_gmt":"2025-05-29T04:17:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204853\/"},"modified":"2025-05-29T12:17:06","modified_gmt":"2025-05-29T04:17:06","slug":"web-%e5%bc%80%e5%8f%91%e4%ba%ba%e5%91%98%e7%9a%84-python-%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204853\/","title":{"rendered":"Web \u5f00\u53d1\u4eba\u5458\u7684 Python \u5165\u95e8"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Web \u5f00\u53d1\u4eba\u5458\u7684 Python \u5165\u95e8<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aWeb \u5f00\u53d1\u4eba\u5458\u7684 Python \u5165\u95e8\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318491686739ebd0d5693.jpg\" class=\"aligncenter\"><\/p>\n<p>python \u5df2\u6210\u4e3a\u6700\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e web \u5f00\u53d1\u3001\u6570\u636e\u5206\u6790\u3001\u4eba\u5de5\u667a\u80fd\u7b49\u9886\u57df\u3002\u5bf9\u4e8e web \u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0cpython \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6846\u67b6\u3001\u7b80\u5355\u7684\u8bed\u6cd5\u548c\u5145\u6ee1\u6d3b\u529b\u7684\u5e93\u751f\u6001\u7cfb\u7edf\u3002\u5982\u679c\u60a8\u662f\u4e00\u540d\u5e0c\u671b\u6269\u5c55\u5230\u540e\u7aef\u5f00\u53d1\u7684 web \u5f00\u53d1\u4eba\u5458\uff0cpython \u662f\u4e00\u4e2a\u7edd\u4f73\u7684\u9009\u62e9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5f00\u59cb\u3002<\/p>\n<ol>\n<li>\u4f5c\u4e3a web \u5f00\u53d1\u4eba\u5458\u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 python\uff1f python \u7684\u53ef\u8bfb\u6027\u3001\u7b80\u5355\u6027\u548c\u591a\u529f\u80fd\u6027\u4f7f\u5176\u6210\u4e3a web \u5f00\u53d1\u7684\u7406\u60f3\u9009\u62e9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u597d\u5904\uff1a<\/li>\n<\/ol>\n<p>\u6613\u4e8e\u5b66\u4e60\uff1apython \u7684\u8bed\u6cd5\u6e05\u6670\u7b80\u5355\uff0c\u5982\u679c\u60a8\u662f\u540e\u7aef\u5f00\u53d1\u65b0\u624b\uff0c\u5b83\u4f1a\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7b2c\u4e00\u8bed\u8a00\u3002<\/p>\n<p>\u591a\u529f\u80fd\u6846\u67b6\uff1apython \u63d0\u4f9b\u4e86 django \u548c flask \u7b49\u6846\u67b6\uff0c\u5b83\u4eec\u7b80\u5316\u4e86\u8def\u7531\u3001\u6570\u636e\u5e93\u4ea4\u4e92\u548c\u8eab\u4efd\u9a8c\u8bc1\u7b49\u540e\u7aef\u4efb\u52a1\u3002<\/p>\n<p>\u4e30\u5bcc\u7684\u5e93\uff1apython\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5305\u63d0\u4f9b\u4e86\u7f51\u9875\u6293\u53d6\u3001\u6570\u636e\u5904\u7406\u3001\u6d4b\u8bd5\u7b49\u5de5\u5177\u3002<\/p>\n<p>\u53ef\u6269\u5c55\u4e14\u5b89\u5168\uff1apython \u7528\u4e8e\u4e3b\u8981 web \u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u5176\u6210\u4e3a\u53ef\u6269\u5c55\u3001\u5b89\u5168\u7684 web \u5f00\u53d1\u7684\u53ef\u9760\u9009\u62e9\u3002<\/p>\n<ol>\n<li>\u8bbe\u7f6e python \u8fdb\u884c web \u5f00\u53d1 \u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u8ba1\u7b97\u673a\u4e0a\u8bbe\u7f6e python\uff1a<\/li>\n<\/ol>\n<p>\u5b89\u88c5 python\uff1a\u4ece python.org \u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u52fe\u9009\u5c06python\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84\u7684\u9009\u9879\u3002<\/p>\n<p>\u5b89\u88c5 pip\uff1apip \u662f python \u7684\u5305\u7ba1\u7406\u5668\uff0cpython \u9644\u5e26\uff0c\u4f46\u8bf7\u786e\u4fdd\u4f7f\u7528 pip install &#8211;upgrade pip \u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<p>\u8bbe\u7f6e\u865a\u62df\u73af\u5883\uff1a\u865a\u62df\u73af\u5883\u5141\u8bb8\u60a8\u5355\u72ec\u7ba1\u7406\u6bcf\u4e2a\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u907f\u514d\u51b2\u7a81\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\uff1a<\/p>\n<pre>python -m venv myprojectenv\nsource myprojectenv\/bin\/activate  # on macos\/linux\nmyprojectenv\\scripts\\activate  # on windows\n<\/pre>\n<ol>\n<li>\u63a2\u7d22 web \u6846\u67b6 python \u6709\u591a\u79cd web \u6846\u67b6\uff0c\u4f46 django \u548c flask \u662f\u6700\u53d7\u6b22\u8fce\u7684\uff1a<\/li>\n<\/ol>\n<p>flask\uff1aflask \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u7075\u6d3b\u7684\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u4e2d\u5c0f\u578b\u9879\u76ee\u3002\u5bf9\u4e8e\u559c\u6b22\u7b80\u5355\u8bbe\u7f6e\u548c\u63a7\u5236\u7279\u5b9a\u7ec4\u4ef6\u7684\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0c\u5b83\u662f\u5b8c\u7f8e\u7684\u9009\u62e9\u3002<\/p>\n<pre>\npip install flask\nbasic flask example:\n\npython\ncopy code\nfrom flask import flask\n\napp = flask(__name__)\n\n@app.route('\/')\ndef home():\n    return \"hello, flask!\"\n\nif __name__ == \"__main__\":\n    app.run(debug=true)\n<\/pre>\n<p>django\uff1adjango \u662f\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684\u6846\u67b6\uff0c\u5e26\u6709\u7528\u4e8e\u8eab\u4efd\u9a8c\u8bc1\u3001orm \u548c\u7ba1\u7406\u754c\u9762\u7684\u5185\u7f6e\u5de5\u5177\u3002\u5b83\u975e\u5e38\u9002\u5408\u5177\u6709\u590d\u6742\u8981\u6c42\u7684\u5927\u578b\u9879\u76ee\u3002<\/p>\n<pre>pip install django\nstarting a django project:\n\nbash\ncopy code\ndjango-admin startproject myproject\ncd myproject\npython manage.py runserver\n<\/pre>\n<p>\u8fd9\u4e24\u4e2a\u6846\u67b6\u90fd\u9644\u5e26\u4e86\u4e30\u5bcc\u7684\u6587\u6863\u548c\u6d3b\u8dc3\u7684\u793e\u533a\uff0c\u56e0\u6b64\u8bf7\u6839\u636e\u60a8\u7684\u9879\u76ee\u9700\u6c42\u9009\u62e9\u4e00\u4e2a\u3002<\/p>\n<ol>\n<li>\u8fde\u63a5\u5230\u6570\u636e\u5e93 python\u652f\u6301\u5f88\u591a\u6570\u636e\u5e93\uff0c\u4f46web\u5f00\u53d1\u6700\u5e38\u7528\u7684\u662f\uff1a<\/li>\n<\/ol>\n<p>sqlite\uff1a\u5185\u7f6e\u4e8e python \u4e2d\uff0c\u9002\u5408\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u3002<br \/> postgresql\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u5f00\u6e90\u6570\u636e\u5e93\uff0c\u7ecf\u5e38\u4e0e django \u4e00\u8d77\u4f7f\u7528\u3002<br \/> mongodb\uff1a\u4e00\u79cd\u6df1\u53d7\u73b0\u4ee3\u53ef\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u6b22\u8fce\u7684 nosql \u6570\u636e\u5e93\u3002<\/p>\n<p>\u5728 flask \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 sqlalchemy \u8fdb\u884c\u6570\u636e\u5e93\u7ba1\u7406\uff0c\u800c django \u6709\u81ea\u5df1\u7684\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668 (orm)\uff0c\u65e0\u9700\u7f16\u5199 sql \u5373\u53ef\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002<\/p>\n<ol>\n<li>\u521b\u5efa rest api \u521b\u5efa rest api \u662f web \u5f00\u53d1\u4e2d\u7684\u4e00\u9879\u5e38\u89c1\u4efb\u52a1\u3002 python \u6846\u67b6\u4f7f\u8fd9\u53d8\u5f97\u7b80\u5355\uff1a<\/li>\n<\/ol>\n<p>\u5728 flask \u4e2d\uff1a\u4f7f\u7528 flask-restful \u6765\u7b80\u5316 api \u7684\u521b\u5efa\u3002<\/p>\n<pre>\nfrom flask import flask, jsonify, request\n\napp = flask(__name__)\n\n@app.route('\/api\/data', methods=['get'])\ndef get_data():\n    data = {\"message\": \"hello, api!\"}\n    return jsonify(data)\n\nif __name__ == '__main__':\n    app.run(debug=true)\n<\/pre>\n<p>\u5728 django \u4e2d\uff1a\u4f7f\u7528 django rest framework (drf) \u5728 django \u4e2d\u521b\u5efa rest api\u3002<\/p>\n<pre>pip install djangorestframework\n<\/pre>\n<ol>\n<li>\u90e8\u7f72\u60a8\u7684 web \u5e94\u7528\u7a0b\u5e8f \u4e00\u65e6\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u51c6\u5907\u5c31\u7eea\uff0c\u90e8\u7f72\u5b83\u5c06\u4f7f\u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u5b83\u3002\u90e8\u7f72 python web \u5e94\u7528\u7a0b\u5e8f\u7684\u5e38\u89c1\u9009\u9879\u5305\u62ec\uff1a<\/li>\n<\/ol>\n<p>heroku\uff1a\u5bf9\u4e8e\u5c0f\u578b\u5e94\u7528\u7a0b\u5e8f\u514d\u8d39\u4e14\u6613\u4e8e\u8bbe\u7f6e\u3002<br \/> digitalocean \u548c aws\uff1a\u5b9e\u73b0\u66f4\u591a\u63a7\u5236\u548c\u7075\u6d3b\u6027\u3002<br \/> vercel\uff1a\u975e\u5e38\u9002\u5408\u90e8\u7f72\u5177\u6709\u65e0\u670d\u52a1\u5668\u652f\u6301\u7684 django \u9879\u76ee\u3002<br \/> \u5bf9\u4e8e\u90e8\u7f72\uff0cflask \u548c django \u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u5728\u751f\u4ea7\u4e2d\u4f7f\u7528 wsgi \u670d\u52a1\u5668\uff0c\u4f8b\u5982 gunicorn\u3002<\/p>\n<ol>\n<li>\u901a\u8fc7\u9879\u76ee\u57f9\u517b\u60a8\u7684\u6280\u80fd \u719f\u80fd\u751f\u5de7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9002\u5408\u521d\u5b66\u8005\u7684\u9879\u76ee\u60f3\u6cd5\uff0c\u53ef\u5e2e\u52a9\u60a8\u57f9\u517b\u6280\u80fd\uff1a<\/li>\n<\/ol>\n<p>\u4e2a\u4eba\u535a\u5ba2\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u535a\u5ba2\uff0c\u60a8\u53ef\u4ee5\u5728\u5176\u4e2d\u64b0\u5199\u548c\u53d1\u5e03\u5e16\u5b50\u3002<br \/> \u5f85\u529e\u4e8b\u9879\u5217\u8868\uff1a\u7528\u4e8e\u6dfb\u52a0\u3001\u5220\u9664\u548c\u66f4\u65b0\u5f85\u529e\u4e8b\u9879\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5929\u6c14\u5e94\u7528\uff1a\u4f7f\u7528 api \u83b7\u53d6\u548c\u663e\u793a\u5929\u6c14\u6570\u636e\u3002<br \/> restful api\uff1a\u4e3a\u4ea7\u54c1\u6216\u7528\u6237\u7b49\u8d44\u6e90\u6784\u5efa api\uff0c\u5177\u6709 crud\uff08\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u3001\u5220\u9664\uff09\u529f\u80fd\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><br \/> python \u662f\u4e00\u79cd\u5f3a\u5927\u7684 web \u5f00\u53d1\u8bed\u8a00\uff0c\u63d0\u4f9b\u7075\u6d3b\u7684\u6846\u67b6\u548c\u7b80\u5355\u7684\u8bed\u6cd5\u3002\u901a\u8fc7\u5b66\u4e60 python\u3001flask \u6216 django\uff0c\u60a8\u53ef\u4ee5\u6269\u5c55\u60a8\u7684\u6280\u80fd\u5e76\u6784\u5efa\u53ef\u6269\u5c55\u3001\u9ad8\u6548\u7684 web \u5e94\u7528\u7a0b\u5e8f\u3002\u65e0\u8bba\u60a8\u662f\u521b\u5efa\u5c0f\u578b api \u8fd8\u662f\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\uff0cpython \u90fd\u6709\u5de5\u5177\u548c\u793e\u533a\u652f\u6301\u6765\u5e2e\u52a9\u60a8\u53d6\u5f97\u6210\u529f\u3002\u5f00\u59cb\u5c1d\u8bd5\u5e76\u89c2\u5bdf\u60a8\u7684 web \u5f00\u53d1\u6280\u80fd\u7684\u589e\u957f\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aWeb \u5f00\u53d1\u4eba\u5458\u7684 Python \u5165\u95e8\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web \u5f00\u53d1\u4eba\u5458\u7684 Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204853","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204853"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204853\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}