{"id":24541,"date":"2024-11-21T14:11:18","date_gmt":"2024-11-21T06:11:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24541\/"},"modified":"2024-11-21T14:11:18","modified_gmt":"2024-11-21T06:11:18","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87webman%e5%ae%9e%e7%8e%b0%e7%bd%91%e7%ab%99%e7%9a%84%e5%86%85%e5%ae%b9%e7%ae%a1%e7%90%86%e5%92%8c%e5%8f%91%e5%b8%83%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24541\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7Webman\u5b9e\u73b0\u7f51\u7ad9\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169304314370829.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u901a\u8fc7Webman\u5b9e\u73b0\u7f51\u7ad9\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\u63d2\u56fe\" alt=\"\u5982\u4f55\u901a\u8fc7Webman\u5b9e\u73b0\u7f51\u7ad9\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u901a\u8fc7Webman\u5b9e\u73b0\u7f51\u7ad9\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf<\/p>\n<p>Webman\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u8bed\u8a00\u5f00\u53d1\u7684Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\u548c\u63d2\u4ef6\uff0c\u5176\u4e2d\u5305\u62ec\u4e00\u4e2a\u7528\u6237\u53cb\u597d\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7ad9\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5Webman<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Webman\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u547d\u4ee4\u884c\u4e2d\u5b89\u88c5Webman\uff1a<\/p>\n<pre>pip install webman<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efaWeb\u5e94\u7528<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aapp.py\u7684\u6587\u4ef6\uff1a<\/p>\n<pre>from webman import Webman\n\napp = Webman()\n\n\n@app.route('\/')\ndef index(request, response):\n    response.text = 'Hello Webman!'\n\n\nif __name__ == '__main__':\n    app.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aapp\u7684Web\u5e94\u7528\uff0c\u5e76\u5728\u6839\u8def\u5f84\uff08&#8217;\/&#8217;\uff09\u4e0a\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5c06response\u5bf9\u8c61\u7684text\u5c5e\u6027\u8bbe\u7f6e\u4e3a&#8217;Hello Webman!&#8217;\uff0c\u8868\u793a\u5c06\u8be5\u6587\u672c\u4f5c\u4e3a\u54cd\u5e94\u8fd4\u56de\u7ed9\u7528\u6237\u3002<\/p>\n<ol>\n<li>\u8fd0\u884cWeb\u5e94\u7528<\/li>\n<\/ol>\n<p>\u4fdd\u5b58app.py\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cWeb\u5e94\u7528\uff1a<\/p>\n<pre>python app.py<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u5e76\u5728URL\u5730\u5740\u680f\u4e2d\u8f93\u5165http:\/\/localhost:8000\uff0c\u4f60\u5c06\u770b\u5230\u663e\u793a&#8217;Hello Webman!&#8217;\u7684\u9875\u9762\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u7f51\u7ad9\u5185\u5bb9\u7ba1\u7406\u9875\u9762<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u7f51\u7ad9\u5185\u5bb9\u7684\u9875\u9762\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u66f4\u65b0app.py\u6587\u4ef6\uff1a<\/p>\n<pre>from webman import Webman\nfrom webman.middleware import SessionMiddleware\n\napp = Webman()\napp.use(SessionMiddleware())\n\n\n@app.route('\/')\ndef index(request, response):\n    response.text = 'Hello Webman!'\n\n\n@app.route('\/admin')\ndef admin(request, response):\n    session = request.session\n    if session.get('logged_in'):\n        response.text = 'Welcome to the admin page!'\n    else:\n        response.redirect('\/login')\n\n\n@app.route('\/login')\ndef login(request, response):\n    session = request.session\n    if request.method == 'POST':\n        username = request.params.get('username')\n        password = request.params.get('password')\n        if username == 'admin' and password == 'password':\n            session['logged_in'] = True\n            response.redirect('\/admin')\n        else:\n            response.redirect('\/login')\n    else:\n        response.send_file('login.html')\n\n\nif __name__ == '__main__':\n    app.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5f15\u5165\u4e86SessionMiddleware\u4e2d\u95f4\u4ef6\uff0c\u7528\u4e8e\u7ba1\u7406\u7528\u6237\u4f1a\u8bdd\u3002\u540c\u65f6\uff0c\u6211\u4eec\u521b\u5efa\u4e863\u4e2a\u5904\u7406\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u663e\u793a\u4e3b\u9875\u3001\u7ba1\u7406\u5458\u9875\u9762\u548c\u767b\u5f55\u9875\u9762\u3002\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528request.session\u5bf9\u8c61\u6765\u5b58\u50a8\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7\u5224\u65ad\u7528\u6237\u662f\u5426\u5df2\u767b\u5f55\u6765\u51b3\u5b9a\u662f\u5426\u663e\u793a\u7ba1\u7406\u5458\u9875\u9762\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u767b\u5f55\u8ba4\u8bc1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e3a\u4e86\u5b89\u5168\u8003\u8651\uff0c\u6211\u4eec\u5e94\u8be5\u4f7f\u7528\u66f4\u52a0\u4e25\u683c\u548c\u590d\u6742\u7684\u8ba4\u8bc1\u65b9\u5f0f\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u767b\u5f55\u9875\u9762<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u767b\u5f55\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u767b\u5f55\u9875\u9762\u3002\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3alogin.html\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230\u8be5\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre>\n\n\n\n&lt;h2&gt;Login&lt;\/h2&gt;\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    Username: <\/p>\n<p> Password: <\/p>\n<p>   <\/p>\n<p>\u8be5HTML\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u8868\u5355\uff0c\u8868\u5355\u7684action\u5c5e\u6027\u6307\u5411\u767b\u5f55\u5904\u7406\u51fd\u6570\u7684URL\u5730\u5740\uff0cmethod\u5c5e\u6027\u4e3apost\u3002\u7528\u6237\u5728\u767b\u5f55\u8868\u5355\u4e2d\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u540e\uff0c\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\uff0c\u8868\u5355\u6570\u636e\u5c06\u88ab\u53d1\u9001\u5230\u767b\u5f55\u5904\u7406\u51fd\u6570\u3002\u767b\u5f55\u5904\u7406\u51fd\u6570\u5c06\u6839\u636e\u7528\u6237\u540d\u548c\u5bc6\u7801\u8fdb\u884c\u9a8c\u8bc1\uff0c\u5e76\u5728\u9a8c\u8bc1\u6210\u529f\u540e\u5c06\u7528\u6237\u4f1a\u8bdd\u8bbe\u7f6e\u4e3a\u5df2\u767b\u5f55\u72b6\u6001\u3002<\/p>\n<ol>\n<li>\u8fd0\u884cWeb\u5e94\u7528<\/li>\n<\/ol>\n<p>\u4fdd\u5b58app.py\u548clogin.html\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cWeb\u5e94\u7528\uff1a<\/p>\n<pre>python app.py<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u5e76\u5728URL\u5730\u5740\u680f\u4e2d\u8f93\u5165http:\/\/localhost:8000\uff0c\u4f60\u5c06\u770b\u5230\u663e\u793a&#8217;Hello Webman!&#8217;\u7684\u9875\u9762\u3002\u70b9\u51fb\u9875\u9762\u4e2d\u7684&#8217;Login&#8217;\u94fe\u63a5\uff0c\u8fdb\u5165\u767b\u5f55\u9875\u9762\u3002\u8f93\u5165\u7528\u6237\u540d\u4e3a&#8217;admin&#8217;\uff0c\u5bc6\u7801\u4e3a&#8217;password&#8217;\uff0c\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\u3002\u5982\u679c\u7528\u6237\u540d\u548c\u5bc6\u7801\u5339\u914d\uff0c\u4f60\u5c06\u88ab\u91cd\u5b9a\u5411\u5230\u7ba1\u7406\u5458\u9875\u9762\uff0c\u663e\u793a&#8217;Welcome to the admin page!&#8217;\u7684\u6d88\u606f\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528Webman\u6784\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7ad9\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\u3002\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u7f51\u7ad9\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u521b\u5efa\u3001\u7f16\u8f91\u548c\u5220\u9664\u9875\u9762\u3001\u4e0a\u4f20\u548c\u7ba1\u7406\u6587\u4ef6\u7b49\u7b49\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u901a\u8fc7Webman\u5b9e\u73b0\u7f51\u7ad9\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7Webman\u5b9e\u73b0\u7f51\u7ad9\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf Webman\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u8bed\u8a00\u5f00\u53d1\u7684Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\u548c\u63d2\u4ef6\uff0c\u5176\u4e2d\u5305\u62ec\u4e00\u4e2a\u7528\u6237\u53cb\u597d\u7684\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7ad9\u5185\u5bb9\u7ba1\u7406\u548c\u53d1\u5e03\u7cfb\u7edf\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b9e\u73b0\u8fc7\u7a0b\u3002 \u5b89\u88c5Webman \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Webman\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u547d\u4ee4\u884c\u4e2d\u5b89\u88c5Webman\uff1a pip install webman \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efaWeb\u5e94\u7528 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aapp.py\u7684\u6587\u4ef6\uff1a from webman import Webman app = Webman() @app.route(&#8216;\/&#8217;) def index(request, response): response.text = &#8216;Hello Webman!&#8217; if __name__ == &#8216;__main__&#8217;: app.run() \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aapp\u7684Web\u5e94\u7528\uff0c\u5e76\u5728\u6839\u8def\u5f84\uff08&#8217;\/&#8217;\uff09\u4e0a\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u3002\u5728\u8fd9\u4e2a\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5c06response\u5bf9\u8c61\u7684text\u5c5e\u6027\u8bbe\u7f6e\u4e3a&#8217;Hello Webman!&#8217;\uff0c\u8868\u793a\u5c06\u8be5\u6587\u672c\u4f5c\u4e3a\u54cd\u5e94\u8fd4\u56de\u7ed9\u7528\u6237\u3002 \u8fd0\u884cWeb\u5e94\u7528 \u4fdd\u5b58app.py\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cWeb\u5e94\u7528\uff1a python app.py \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u5e76\u5728URL\u5730\u5740\u680f\u4e2d\u8f93\u5165http:\/\/localhost:8000\uff0c\u4f60\u5c06\u770b\u5230\u663e\u793a&#8217;Hello Webman!&#8217;\u7684\u9875\u9762\u3002 \u521b\u5efa\u7f51\u7ad9\u5185\u5bb9\u7ba1\u7406\u9875\u9762 \u73b0\u5728\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u7f51\u7ad9\u5185\u5bb9\u7684\u9875\u9762\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u66f4\u65b0app.py\u6587\u4ef6\uff1a from webman import Webman from webman.middleware import SessionMiddleware app = Webman() app.use(SessionMiddleware()) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24541","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24541"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24541\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}