{"id":24465,"date":"2024-11-21T08:42:45","date_gmt":"2024-11-21T00:42:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24465\/"},"modified":"2024-11-21T08:42:45","modified_gmt":"2024-11-21T00:42:45","slug":"%e4%ba%86%e8%a7%a3webman%e7%9a%84%e9%87%8d%e8%a6%81%e7%89%b9%e6%80%a7%e5%92%8c%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24465\/","title":{"rendered":"\u4e86\u89e3Webman\u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169295554714178.jpg\" class=\"aligncenter\" title=\"\u4e86\u89e3Webman\u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd\u63d2\u56fe\" alt=\"\u4e86\u89e3Webman\u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u4e86\u89e3Webman\u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd<\/p>\n<p>Webman \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python Web \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u91cd\u8981\u7684\u7279\u6027\u548c\u529f\u80fd\uff0c\u4f7f\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u9ad8\u6027\u80fd\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Webman \u7684\u4e00\u4e9b\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u4f9b\u8bfb\u8005\u53c2\u8003\u3002<\/p>\n<p>\u4e00\u3001\u5f02\u6b65\u5904\u7406\u80fd\u529b<\/p>\n<p>Webman \u5177\u6709\u5f3a\u5927\u7684\u5f02\u6b65\u5904\u7406\u80fd\u529b\uff0c\u53ef\u4ee5\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u548c\u5927\u91cf\u7684 I\/O \u64cd\u4f5c\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5f02\u6b65\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>import webman\n\nasync def handle_request(request):\n    # \u5f02\u6b65\u5904\u7406\u8bf7\u6c42\n    response = await some_async_function(request)\n    return response\n\napp = webman.Application()\napp.add_route(\"\/\", handle_request)\napp.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0chandle_request \u51fd\u6570\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u901a\u8fc7 await \u5173\u952e\u5b57\u6765\u7b49\u5f85\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u8bf7\u6c42\u88ab\u963b\u585e\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u4e8c\u3001\u8def\u7531\u548c\u8bf7\u6c42\u5904\u7406<\/p>\n<p>Webman \u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u8def\u7531\u7cfb\u7edf\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9a\u4e49\u4e0d\u540c URL \u8def\u5f84\u5bf9\u5e94\u7684\u8bf7\u6c42\u5904\u7406\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8def\u7531\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>import webman\n\ndef index(request):\n    return webman.Response(\"Hello, Webman!\")\n\ndef about(request):\n    return webman.Response(\"This is about page.\")\n\napp = webman.Application()\napp.add_route(\"\/\", index)\napp.add_route(\"\/about\", about)\napp.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0cindex \u51fd\u6570\u548c about \u51fd\u6570\u5206\u522b\u5904\u7406\u6839\u8def\u5f84\u548c\/about\u8def\u5f84\u7684\u8bf7\u6c42\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<p>\u4e09\u3001\u6a21\u677f\u5f15\u64ce\u652f\u6301<\/p>\n<p>Webman \u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u6a21\u677f\u5f15\u64ce\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6e32\u67d3\u52a8\u6001\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>import webman\nfrom webman import TemplateEngine\n\ndef index(request):\n    data = {\n        \"name\": \"Webman\",\n        \"version\": \"1.0\"\n    }\n    template = TemplateEngine.render(\"index.html\", data)\n    return webman.Response(template)\n\napp = webman.Application()\napp.add_route(\"\/\", index)\napp.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0cdata \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4e86\u5728\u6a21\u677f\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u3002TemplateEngine.render \u65b9\u6cd5\u7528\u4e8e\u6e32\u67d3\u6a21\u677f\u6587\u4ef6\uff0c\u5e76\u5c06\u6a21\u677f\u4e2d\u7684\u53d8\u91cf\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p>\u56db\u3001\u4e2d\u95f4\u4ef6\u6269\u5c55<\/p>\n<p>Webman \u652f\u6301\u4e2d\u95f4\u4ef6\u6269\u5c55\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u548c\u54cd\u5e94\u4e4b\u95f4\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4e2d\u95f4\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>import webman\n\ndef logger_middleware(request, handler):\n    # \u5728\u8bf7\u6c42\u4e4b\u524d\u8bb0\u5f55\u65e5\u5fd7\n    print(\"Handling request for path: \", request.path)\n    # \u8c03\u7528\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216\u8bf7\u6c42\u5904\u7406\u51fd\u6570\n    response = handler(request)\n    # \u5728\u54cd\u5e94\u4e4b\u540e\u8bb0\u5f55\u65e5\u5fd7\n    print(\"Handled request for path: \", request.path)\n    return response\n\ndef index(request):\n    return webman.Response(\"Hello, Webman!\")\n\napp = webman.Application()\napp.add_middleware(logger_middleware)\napp.add_route(\"\/\", index)\napp.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0clogger_middleware \u662f\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u51fd\u6570\uff0c\u5b83\u5728\u8bf7\u6c42\u4e4b\u524d\u548c\u54cd\u5e94\u4e4b\u540e\u8bb0\u5f55\u4e86\u76f8\u5e94\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<p>\u901a\u8fc7\u4e86\u89e3 Webman \u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u5b83\u7684\u5f3a\u5927\u4e4b\u5904\u3002\u5b83\u63d0\u4f9b\u4e86\u5f02\u6b65\u5904\u7406\u80fd\u529b\u3001\u7b80\u6d01\u7684\u8def\u7531\u7cfb\u7edf\u3001\u7075\u6d3b\u7684\u6a21\u677f\u5f15\u64ce\u652f\u6301\u4ee5\u53ca\u4e2d\u95f4\u4ef6\u6269\u5c55\u673a\u5236\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u66f4\u52a0\u9ad8\u6548\u5730\u5f00\u53d1\u548c\u90e8\u7f72 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u5e0c\u671b\u672c\u6587\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u4e86\u89e3\u548c\u5e94\u7528 Webman\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e86\u89e3Webman\u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd\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>\u4e86\u89e3Webman\u7684\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd Webman \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python Web \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u91cd\u8981\u7684\u7279\u6027\u548c\u529f\u80fd\uff0c\u4f7f\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u9ad8\u6027\u80fd\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Webman \u7684\u4e00\u4e9b\u91cd\u8981\u7279\u6027\u548c\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u4f9b\u8bfb\u8005\u53c2\u8003\u3002 \u4e00\u3001\u5f02\u6b65\u5904\u7406\u80fd\u529b Webman \u5177\u6709\u5f3a\u5927\u7684\u5f02\u6b65\u5904\u7406\u80fd\u529b\uff0c\u53ef\u4ee5\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u548c\u5927\u91cf\u7684 I\/O \u64cd\u4f5c\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5f02\u6b65\u5904\u7406\u7684\u793a\u4f8b\uff1a import webman async def handle_request(request): # \u5f02\u6b65\u5904\u7406\u8bf7\u6c42 response = await some_async_function(request) return response app = webman.Application() app.add_route(&#8220;\/&#8221;, handle_request) app.run() \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0chandle_request \u51fd\u6570\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u901a\u8fc7 await \u5173\u952e\u5b57\u6765\u7b49\u5f85\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u8bf7\u6c42\u88ab\u963b\u585e\uff0c\u63d0\u9ad8\u5e94\u7528\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002 \u4e8c\u3001\u8def\u7531\u548c\u8bf7\u6c42\u5904\u7406 Webman \u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u8def\u7531\u7cfb\u7edf\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9a\u4e49\u4e0d\u540c URL \u8def\u5f84\u5bf9\u5e94\u7684\u8bf7\u6c42\u5904\u7406\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8def\u7531\u7684\u793a\u4f8b\uff1a import webman def index(request): return webman.Response(&#8220;Hello, Webman!&#8221;) def about(request): return webman.Response(&#8220;This [&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-24465","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24465","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=24465"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24465\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}