{"id":24076,"date":"2024-11-21T17:43:21","date_gmt":"2024-11-21T09:43:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24076\/"},"modified":"2024-11-21T17:43:21","modified_gmt":"2024-11-21T09:43:21","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87webman%e6%a1%86%e6%9e%b6%e5%ae%9e%e7%8e%b0%e7%94%a8%e6%88%b7%e8%ae%a4%e8%af%81%e5%92%8c%e6%8e%88%e6%9d%83%e5%8a%9f%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24076\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7Webman\u6846\u67b6\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\uff1f"},"content":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7webman\u6846\u67b6\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\uff1f<\/p>\n<p>Webman \u662f\u4e00\u6b3e\u57fa\u4e8e Python \u7684\u8f7b\u91cf\u7ea7 Web \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u6269\u5c55\u6027\u3002\u5728\u5f00\u53d1\u4e2d\uff0c\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u662f\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Webman \u6846\u67b6\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5 Webman<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5 Webman\u3002\u53ef\u4ee5\u4f7f\u7528 pip \u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<pre>pip install webman<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521d\u59cb\u5316 Webman \u5e94\u7528<\/li>\n<\/ol>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Python \u6587\u4ef6\uff0c\u4f8b\u5982 app.py\uff0c\u5e76\u5bfc\u5165 Webman \u7684\u76f8\u5173\u6a21\u5757\uff1a<\/p>\n<pre>from webman import Webman, handler\n\napp = Webman()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u6dfb\u52a0\u7528\u6237\u8ba4\u8bc1\u529f\u80fd<\/li>\n<\/ol>\n<p>\u5728 Webman \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u529f\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u51fd\u6570\u6765\u8fdb\u884c\u8ba4\u8bc1\uff1a<\/p>\n<pre>def authenticate(handler_func):\n    def wrapper(request, *args, **kwargs):\n        # \u5728\u8fd9\u91cc\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u903b\u8f91\n        if request.get_cookie('username') == 'admin':\n            return handler_func(request, *args, **kwargs)\n        else:\n            return 'Unauthorized', 401  # \u8fd4\u56de\u672a\u6388\u6743\u7684 HTTP \u72b6\u6001\u7801\n\n    return wrapper<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u5728\u9700\u8981\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u7684\u8bf7\u6c42\u5904\u7406\u51fd\u6570\u4e0a\u52a0\u4e0a @authenticate \u88c5\u9970\u5668\uff1a<\/p>\n<pre>@app.route('\/protected')\n@authenticate\ndef protected_handler(request):\n    return 'Protected content'<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u6dfb\u52a0\u7528\u6237\u6388\u6743\u529f\u80fd<\/li>\n<\/ol>\n<p>\u9664\u4e86\u7528\u6237\u8ba4\u8bc1\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u7528\u6237\u6388\u6743\u529f\u80fd\u3002\u5728 Webman \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u53c2\u6570\u6765\u4f20\u9012\u7528\u6237\u89d2\u8272\u6216\u6743\u9650\u7b49\u4fe1\u606f\u3002\u540c\u6837\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u51fd\u6570\u6765\u8fdb\u884c\u6388\u6743\uff1a<\/p>\n<pre>def authorize(roles):\n    def decorator(handler_func):\n        def wrapper(request, *args, **kwargs):\n            # \u5728\u8fd9\u91cc\u8fdb\u884c\u7528\u6237\u6388\u6743\u903b\u8f91\n            user_roles = ['admin']\n            if set(user_roles).intersection(set(roles)):\n                return handler_func(request, *args, **kwargs)\n            else:\n                return 'Forbidden', 403  # \u8fd4\u56de\u7981\u6b62\u8bbf\u95ee\u7684 HTTP \u72b6\u6001\u7801\n\n        return wrapper\n\n    return decorator<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u4f7f\u7528 @authorize \u88c5\u9970\u5668\u6765\u9650\u5236\u7528\u6237\u89d2\u8272\u8bbf\u95ee\uff1a<\/p>\n<pre>@app.route('\/admin')\n@authenticate\n@authorize(['admin'])\ndef admin_handler(request):\n    return 'Admin content'<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8fd0\u884c Webman \u5e94\u7528<\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u6dfb\u52a0\u4e00\u4e2a\u542f\u52a8\u6587\u4ef6\uff0c\u4f8b\u5982 main.py\uff1a<\/p>\n<pre>from app import app\n\nif __name__ == '__main__':\n    app.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd0\u884c\u5e94\u7528\uff1a<\/p>\n<pre>python main.py<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u5b8c\u6210\u4e86\u57fa\u4e8e Webman \u6846\u67b6\u7684\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\u5b9e\u73b0\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u88ab\u4fdd\u62a4\u7684\u8def\u7531\u65f6\uff0cWebman \u5c06\u4f1a\u5148\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\uff0c\u7136\u540e\u6839\u636e\u7528\u6237\u89d2\u8272\u6765\u8fdb\u884c\u6388\u6743\u64cd\u4f5c\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 Webman \u6846\u67b6\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u800c\u7075\u6d3b\u5730\u5bf9\u8bf7\u6c42\u8fdb\u884c\u8ba4\u8bc1\u548c\u6388\u6743\u7684\u5904\u7406\u3002Webman \u63d0\u4f9b\u7684\u8fd9\u4e9b\u529f\u80fd\u8ba9\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u5730\u6784\u5efa\u5b89\u5168\u53ef\u9760\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u901a\u8fc7Webman\u6846\u67b6\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\uff1f\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\u6846\u67b6\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\uff1f Webman \u662f\u4e00\u6b3e\u57fa\u4e8e Python \u7684\u8f7b\u91cf\u7ea7 Web \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u6269\u5c55\u6027\u3002\u5728\u5f00\u53d1\u4e2d\uff0c\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u662f\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Webman \u6846\u67b6\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002 \u5b89\u88c5 Webman \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5 Webman\u3002\u53ef\u4ee5\u4f7f\u7528 pip \u547d\u4ee4\u6765\u5b89\u88c5\uff1a pip install webman \u767b\u5f55\u540e\u590d\u5236 \u521d\u59cb\u5316 Webman \u5e94\u7528 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Python \u6587\u4ef6\uff0c\u4f8b\u5982 app.py\uff0c\u5e76\u5bfc\u5165 Webman \u7684\u76f8\u5173\u6a21\u5757\uff1a from webman import Webman, handler app = Webman() \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u7528\u6237\u8ba4\u8bc1\u529f\u80fd \u5728 Webman \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u529f\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u51fd\u6570\u6765\u8fdb\u884c\u8ba4\u8bc1\uff1a def authenticate(handler_func): def wrapper(request, *args, **kwargs): # \u5728\u8fd9\u91cc\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u903b\u8f91 if request.get_cookie(&#8216;username&#8217;) == &#8216;admin&#8217;: return handler_func(request, [&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-24076","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24076","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=24076"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24076\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}