{"id":2767,"date":"2013-05-21T09:27:38","date_gmt":"2013-05-21T01:27:38","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2767"},"modified":"2013-05-21T09:27:38","modified_gmt":"2013-05-21T01:27:38","slug":"tornado%e4%bd%bf%e7%94%a8%e7%bb%8f%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2767.html","title":{"rendered":"Tornado\u4f7f\u7528\u7ecf\u9a8c"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u7f51\u7ad9\u7684\u540e\u7aef\u5f00\u53d1\u3002\u56e0\u4e3a\u521d\u671f\u53ea\u6709\u6211\u4e00\u4e2a\u4eba\u505a\uff0c\u6240\u4ee5\u6280\u672f\u9009\u62e9\u4e0a\u5f88\u81ea\u7531\u3002\u5728 web \u670d\u52a1\u5668\u4e0a\u6211\u9009\u62e9\u4e86 Tornado\u3002\u867d\u7136\u66fe\u7ecf\u4e5f\u8bfb\u8fc7\u5b83\u7684\u6e90\u7801\uff0c\u5e76\u505a\u8fc7\u4e00\u4e9b\u5c0f\u7684 demo\uff0c\u4f46\u6bd5\u7adf\u8fd9\u662f\u7b2c\u4e00\u6b21\u5728\u5de5\u4f5c\u4e2d\u4f7f\u7528\uff0c\u96be\u514d\u53c8\u53d1\u73b0\u4e86\u4e00\u4e9b\u503c\u5f97\u5206\u4eab\u7684\u4e1c\u897f\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u9996\u5148\u60f3\u8bf4\u7684\u662f\u5b83\u7684\u5b89\u5168\u6027\uff0c\u8fd9\u65b9\u9762\u786e\u5b9e\u80fd\u8ba9\u6211\u611f\u53d7\u5230\u5b83\u7684\u826f\u82e6\u7528\u5fc3\u3002\u8fd9\u4e3b\u8981\u53ef\u4ee5\u5206\u4e3a\u4e24\u70b9\uff1a<\/span><br \/>\n<span style=\"font-size: small;\"> \u9632\u8303\u8de8\u7ad9\u4f2a\u9020\u8bf7\u6c42\uff08Cross-site request forgery\uff0c\u7b80\u79f0 CSRF \u6216 XSRF\uff09\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> CSRF \u7684\u610f\u601d\u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff0c\u653b\u51fb\u8005\u4f2a\u9020\u771f\u5b9e\u7528\u6237\u6765\u53d1\u9001\u8bf7\u6c42\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u67d0\u4e2a\u94f6\u884c\u7f51\u7ad9\u6709\u8fd9\u6837\u7684 URL\uff1a<\/span><br \/>\n<span style=\"font-size: small;\"> http:\/\/bank.example.com\/withdraw?amount=1000000&amp;for=Eve<\/span><br \/>\n<span style=\"font-size: small;\"> \u5f53\u8fd9\u4e2a\u94f6\u884c\u7f51\u7ad9\u7684\u7528\u6237\u8bbf\u95ee\u8be5 URL \u65f6\uff0c\u5c31\u4f1a\u7ed9 Eve \u8fd9\u540d\u7528\u6237\u4e00\u767e\u4e07\u5143\u3002\u7528\u6237\u5f53\u7136\u4e0d\u4f1a\u8f7b\u6613\u5730\u70b9\u51fb\u8fd9\u4e2a URL\uff0c\u4f46\u662f\u653b\u51fb\u8005\u53ef\u4ee5\u5728\u5176\u4ed6\u7f51\u7ad9\u4e0a\u5d4c\u5165\u4e00\u5f20\u4f2a\u9020\u7684\u56fe\u7247\uff0c\u5c06\u56fe\u7247\u5730\u5740\u8bbe\u4e3a\u8be5 URL\uff1a<\/span><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;img src=&quot;http:\/\/bank.example.com\/withdraw?amount=1000000&amp;for=Eve&quot;&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u90a3\u4e48\u5f53\u7528\u6237\u8bbf\u95ee\u90a3\u4e2a\u6076\u610f\u7f51\u7ad9\u65f6\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u5bf9\u8be5 URL \u53d1\u8d77\u4e00\u4e2a GET \u8bf7\u6c42\uff0c\u4e8e\u662f\u5728\u7528\u6237\u6beb\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u767e\u4e07\u5c31\u88ab\u8f6c\u8d70\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8981\u9632\u8303\u4e0a\u8ff0\u653b\u51fb\u5f88\u7b80\u5355\uff0c\u4e0d\u5141\u8bb8\u901a\u8fc7 GET \u8bf7\u6c42\u6765\u6267\u884c\u66f4\u6539\u64cd\u4f5c\uff08\u4f8b\u5982\u8f6c\u8d26\uff09\u5373\u53ef\u3002\u4e0d\u8fc7\u5176\u4ed6\u7c7b\u578b\u7684\u8bf7\u6c42\u7167\u6837\u4e5f\u4e0d\u5b89\u5168\uff0c\u5047\u5982\u653b\u51fb\u8005\u6784\u9020\u8fd9\u6837\u4e00\u4e2a\u8868\u5355\uff1a<\/span><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;form action=&quot;http:\/\/bank.example.com\/withdraw&quot; method=&quot;post&quot;&gt;\r\n    &lt;p&gt;\u8f6c\u53d1\u62bd\u5956\u9001 iPad \u554a\uff01&lt;\/p&gt;\r\n    &lt;input type=&quot;hidden&quot; name=&quot;amount&quot; value=&quot;1000000&quot;&gt;\r\n    &lt;input type=&quot;hidden&quot; name=&quot;for&quot; value=&quot;Eve&quot;&gt;\r\n    &lt;input type=&quot;submit&quot; value=&quot;\u8f6c\u53d1&quot;&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0d\u660e\u771f\u76f8\u7684\u7528\u6237\u70b9\u4e86\u4e0b\u201c\u8f6c\u53d1\u201d\u6309\u94ae\uff0c\u7ed3\u679c\u94b1\u5c31\u88ab\u8f6c\u8d70\u4e86\u2026<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8981\u675c\u7edd\u8fd9\u79cd\u60c5\u51b5\uff0c\u5c31\u9700\u8981\u5728\u975e GET \u8bf7\u6c42\u65f6\u6dfb\u52a0\u4e00\u4e2a\u653b\u51fb\u8005\u65e0\u6cd5\u4f2a\u9020\u7684\u5b57\u6bb5\uff0c\u5904\u7406\u8bf7\u6c42\u65f6\u9a8c\u8bc1\u8fd9\u4e2a\u5b57\u6bb5\u662f\u5426\u4fee\u6539\u8fc7\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> Tornado \u7684\u5904\u7406\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u5728\u8bf7\u6c42\u4e2d\u589e\u52a0\u4e86\u4e00\u4e2a\u968f\u673a\u751f\u6210\u7684 _xsrf \u5b57\u6bb5\uff0c\u5e76\u4e14 cookie \u4e2d\u4e5f\u589e\u52a0\u8fd9\u4e2a\u5b57\u6bb5\uff0c\u5728\u63a5\u6536\u8bf7\u6c42\u65f6\uff0c\u6bd4\u8f83\u8fd9 2 \u4e2a\u5b57\u6bb5\u7684\u503c\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u7531\u4e8e\u975e\u672c\u7ad9\u7684\u7f51\u9875\u662f\u4e0d\u80fd\u83b7\u53d6\u6216\u4fee\u6539 cookie \u7684\uff0c\u8fd9\u5c31\u4fdd\u8bc1\u4e86 _xsrf \u65e0\u6cd5\u88ab\u7b2c\u4e09\u65b9\u7f51\u7ad9\u4f2a\u9020\uff08HTTP \u55c5\u63a2\u4f8b\u5916\uff09\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u5f53\u7136\uff0c\u7528\u6237\u81ea\u5df1\u662f\u53ef\u4ee5\u968f\u610f\u83b7\u53d6\u548c\u4fee\u6539 cookie \u7684\uff0c\u4e0d\u8fc7\u8fd9\u5df2\u7ecf\u4e0d\u5c5e\u4e8e CSRF \u7684\u8303\u7574\u4e86\uff1a\u7528\u6237\u81ea\u5df1\u4f2a\u9020\u81ea\u5df1\u6240\u505a\u7684\u4e8b\u60c5\uff0c\u5f53\u7136\u7531\u4ed6\u81ea\u5df1\u6765\u627f\u62c5\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8981\u4f7f\u7528\u8be5\u529f\u80fd\u7684\u8bdd\uff0c\u9700\u8981\u5728\u751f\u6210 tornado.web.Application \u5bf9\u8c61\u65f6\uff0c\u52a0\u4e0a xsrf_cookies=True \u53c2\u6570\uff0c\u8fd9\u4f1a\u7ed9\u7528\u6237\u751f\u6210\u4e00\u4e2a\u540d\u4e3a _xsrf \u7684 cookie \u5b57\u6bb5\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u6b64\u5916\u8fd8\u9700\u8981\u4f60\u5728\u975e GET \u8bf7\u6c42\u7684\u8868\u5355\u91cc\u52a0\u4e0a xsrf_form_html()\uff0c\u5982\u679c\u4e0d\u7528 Tornado \u7684\u6a21\u677f\u7684\u8bdd\uff0c\u5728 tornado.web.RequestHandler \u5185\u90e8\u53ef\u4ee5\u7528 self.xsrf_form_html() \u6765\u751f\u6210\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5bf9\u4e8e AJAX \u8bf7\u6c42\u6765\u8bf4\uff0c\u57fa\u672c\u4e0a\u662f\u4e0d\u9700\u8981\u62c5\u5fc3\u8de8\u7ad9\u7684\uff0c\u6240\u4ee5 Tornado 1.1.1 \u4ee5\u524d\u7684\u7248\u672c\u5e76\u4e0d\u5bf9\u5e26\u6709 X-Requested-With: XMLHTTPRequest \u7684\u8bf7\u6c42\u505a\u9a8c\u8bc1\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u540e\u6765 Google \u7684\u5de5\u7a0b\u5e08\u6307\u51fa\uff0c\u6076\u610f\u7684\u6d4f\u89c8\u5668\u63d2\u4ef6\u53ef\u4ee5\u4f2a\u9020\u8de8\u57df AJAX \u8bf7\u6c42\uff0c\u6240\u4ee5\u4e5f\u5e94\u8be5\u8fdb\u884c\u9a8c\u8bc1\u3002\u5bf9\u6b64\u6211\u4e0d\u7f6e\u53ef\u5426\uff0c\u56e0\u4e3a\u6d4f\u89c8\u5668\u63d2\u4ef6\u7684\u6743\u9650\u53ef\u4ee5\u975e\u5e38\u5927\uff0c\u4f2a\u9020 cookie \u6216\u662f\u76f4\u63a5\u63d0\u4ea4\u8868\u5355\u90fd\u884c\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4e0d\u8fc7\u89e3\u51b3\u529e\u6cd5\u4ecd\u7136\u8981\u8bf4\uff0c\u5176\u5b9e\u53ea\u8981\u4ece cookie \u4e2d\u83b7\u53d6 _xsrf \u5b57\u6bb5\uff0c\u7136\u540e\u5728 AJAX \u8bf7\u6c42\u65f6\u52a0\u4e0a\u8fd9\u4e2a\u53c2\u6570\uff0c\u6216\u8005\u653e\u5728 X-Xsrftoken \u6216 X-Csrftoken \u8bf7\u6c42\u5934\u91cc\u5373\u53ef\u3002\u5acc\u9ebb\u70e6\u7684\u8bdd\uff0c\u53ef\u4ee5\u7528 jQuery \u7684 $.ajaxSetup() \u6765\u5904\u7406\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$.ajaxSetup({\r\n    beforeSend: function(jqXHR, settings) {\r\n        type = settings.type\r\n        if (type != 'GET' &amp;&amp; type != 'HEAD' &amp;&amp; type != 'OPTIONS') {\r\n            var pattern = \/(.+; *)?_xsrf *= *(&#x5B;^;&quot; ]+)\/;\r\n            var xsrf = pattern.exec(document.cookie);\r\n            if (xsrf) {\r\n                jqXHR.setRequestHeader('X-Xsrftoken', xsrf&#x5B;2]);\r\n            }\r\n        }\r\n}});\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6b64\u5916\u518d\u987a\u4fbf\u8c08\u8c08\u8de8\u7ad9\u811a\u672c\uff08Cross-site scripting\uff0c\u7b80\u79f0 XSS\uff09\u3002\u548c CSRF \u76f8\u53cd\u7684\u662f\uff0cXSS \u662f\u5229\u7528\u88ab\u653b\u51fb\u7f51\u7ad9\u81ea\u8eab\u7684\u6f0f\u6d1e\uff0c\u5728\u8be5\u7f51\u7ad9\u4e0a\u6ce8\u5165\u653b\u51fb\u8005\u60f3\u6267\u884c\u7684\u811a\u672c\u4ee3\u7801\uff0c\u8ba9\u6d4f\u89c8\u8be5\u7f51\u7ad9\u7684\u7528\u6237\u6267\u884c\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4e0d\u8fc7\u53ea\u8981\u4e0d\u8ba9\u7528\u6237\u968f\u610f\u8f93\u5165 HTML\uff08\u4f8b\u5982\u5bf9 &lt; \u548c &gt; \u8fdb\u884c\u8f6c\u4e49\uff09\uff0c\u5bf9 HTML \u5143\u7d20\u7684\u5c5e\u6027\u505a\u9a8c\u8bc1\uff08\u4f8b\u5982\u5c5e\u6027\u91cc\u7684\u5f15\u53f7\u8981\u8f6c\u4e49\uff0csrc \u548c \u4e8b\u4ef6\u5904\u7406\u7b49\u5c5e\u6027\u4e0d\u80fd\u968f\u610f\u586b\u5199 JavaScript \u4ee3\u7801\u7b49\uff09\uff0c\u5e76\u68c0\u67e5 CSS\uff08\u542b style \u5c5e\u6027\uff09\u4e2d\u7684 expression \u5373\u53ef\u907f\u514d\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u9632\u6b62\u4f2a\u9020 cookie\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u524d\u9762\u63d0\u5230\u7684 CSRF \u548c XSS \u90fd\u662f\u653b\u51fb\u8005\u5728\u7528\u6237\u4e0d\u77e5\u60c5\u7684\u60c5\u51b5\u4e0b\uff0c\u5192\u7528\u4ed6\u7684\u540d\u4e49\u6765\u8fdb\u884c\u64cd\u4f5c\uff1b\u800c\u4f2a\u9020 cookie \u5219\u662f\u653b\u51fb\u8005\u81ea\u5df1\u4e3b\u52a8\u4f2a\u9020\u5176\u4ed6\u7528\u6237\u6765\u8fdb\u884c\u64cd\u4f5c\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4e3e\u4f8b\u6765\u8bf4\uff0c\u5047\u8bbe\u7f51\u7ad9\u7684\u767b\u5f55\u9a8c\u8bc1\u5c31\u662f\u68c0\u67e5 cookie \u4e2d\u7684\u7528\u6237\u540d\uff0c\u53ea\u8981\u7b26\u5408\u7684\u8bdd\uff0c\u5c31\u8ba4\u4e3a\u8be5\u7528\u6237\u5df2\u767b\u5f55\u3002\u90a3\u4e48\u653b\u51fb\u8005\u53ea\u8981\u5728 cookie \u4e2d\u8bbe\u7f6e username=admin \u4e4b\u7c7b\u7684\u503c\uff0c\u5c31\u53ef\u4ee5\u5192\u5145\u7ba1\u7406\u5458\u6765\u64cd\u4f5c\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8981\u9632\u6b62 cookie \u88ab\u4f2a\u9020\uff0c\u9996\u5148\u9700\u8981\u63d0\u5230\u8bbe\u7f6e cookie \u65f6\u7684\u4e24\u4e2a\u53c2\u6570\uff1asecure \u548c httponly\u3002\u8fd9\u4e24\u4e2a\u53c2\u6570\u5e76\u4e0d\u5728 tornado.web.RequestHandler.set_cookie() \u7684\u53c2\u6570\u5217\u8868\u91cc\uff0c\u800c\u662f\u4f5c\u4e3a\u5173\u952e\u5b57\u53c2\u6570\u4f20\u9012\uff0c\u5e76\u5728 Cookie.Morsel._reserved \u4e2d\u5b9a\u4e49\u7684\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u524d\u8005\u662f\u6307\u8fd9\u4e2a cookie \u53ea\u80fd\u901a\u8fc7\u5b89\u5168\u8fde\u63a5\u4f20\u9012\uff08\u5373 HTTPS\uff09\uff0c\u8fd9\u5c31\u4f7f\u5f97\u55c5\u63a2\u8005\u65e0\u6cd5\u622a\u83b7\u8be5 cookie\uff1b\u540e\u8005\u5219\u8981\u6c42\u5176\u53ea\u80fd\u5728 HTTP \u534f\u8bae\u4e0b\u8bbf\u95ee\uff08\u5373\u65e0\u6cd5\u901a\u8fc7 JavaScript \u6765\u83b7\u53d6 document.cookie \u4e2d\u7684\u8be5\u5b57\u6bb5\uff0c\u5e76\u4e14\u8bbe\u7f6e\u540e\u4e5f\u4e0d\u4f1a\u901a\u8fc7 HTTP \u534f\u8bae\u5411\u670d\u52a1\u5668\u53d1\u9001\uff09\uff0c\u8fd9\u4fbf\u4f7f\u5f97\u653b\u51fb\u8005\u65e0\u6cd5\u7b80\u5355\u5730\u901a\u8fc7 JavaScript \u811a\u672c\u6765\u4f2a\u9020 cookie\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0d\u8fc7\u5bf9\u4e8e\u6076\u610f\u7684\u653b\u51fb\u8005\uff0c\u8fd9\u4e24\u4e2a\u53c2\u6570\u5e76\u4e0d\u80fd\u675c\u7edd cookie \u88ab\u4f2a\u9020\u3002\u4e3a\u6b64\u5c31\u9700\u8981\u5bf9 cookie \u505a\u4e2a\u7b7e\u540d\uff0c\u4e00\u65e6\u88ab\u4fee\u6539\uff0c\u670d\u52a1\u5668\u7aef\u53ef\u4ee5\u5224\u65ad\u51fa\u6765\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> Tornado \u4e2d\u63d0\u4f9b\u4e86 set_secure_cookie() \u8fd9\u4e2a\u65b9\u6cd5\u6765\u5bf9 cookie \u505a\u7b7e\u540d\u3002\u7b7e\u540d\u65f6\u9700\u8981\u63d0\u4f9b\u4e00\u4e32\u79d8\u94a5\uff08\u751f\u6210 tornado.web.Application \u5bf9\u8c61\u65f6\u7684 cookie_secret \u53c2\u6570\uff09\uff0c\u8fd9\u4e2a\u79d8\u94a5\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u6765\u751f\u6210\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nbase64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u968f\u673a\u751f\u6210\uff0c\u4f46\u5982\u679c\u540c\u65f6\u6709\u591a\u4e2a Tornado \u8fdb\u7a0b\u6765\u670d\u52a1\u7684\u8bdd\uff0c\u6216\u8005\u6709\u65f6\u4f1a\u91cd\u542f\u7684\u8bdd\uff0c\u8fd8\u662f\u5171\u7528\u4e00\u4e2a\u5e38\u91cf\u6bd4\u8f83\u597d\uff0c\u5e76\u4e14\u6ce8\u610f\u4e0d\u8981\u6cc4\u9732\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u7b7e\u540d\u7528\u7684\u662f HMAC \u7b97\u6cd5\uff0chash \u7b97\u6cd5\u91c7\u7528\u7684\u662f SHA1\u3002\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u628a cookie \u540d\u3001\u503c\u548c\u65f6\u95f4\u6233\u7684 hash \u4f5c\u4e3a\u7b7e\u540d\uff0c\u518d\u628a\u201c\u503c|\u65f6\u95f4\u6233|\u7b7e\u540d\u201d\u4f5c\u4e3a\u65b0\u7684\u503c\u3002\u8fd9\u6837\u670d\u52a1\u5668\u7aef\u53ea\u8981\u62ff\u79d8\u94a5\u518d\u6b21\u52a0\u5bc6\uff0c\u6bd4\u8f83\u7b7e\u540d\u662f\u5426\u6709\u53d8\u5316\u8fc7\u5373\u53ef\u5224\u65ad\u771f\u4f2a\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u503c\u5f97\u4e00\u63d0\u7684\u662f\u8bfb\u6e90\u7801\u65f6\u8fd8\u53d1\u73b0\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef _time_independent_equals(a, b):\r\n    if len(a) != len(b):\r\n        return False\r\n    result = 0\r\n    if type(a&#x5B;0]) is int:  # python3 byte strings\r\n        for x, y in zip(a, b):\r\n            result |= x ^ y\r\n    else:  # python2\r\n        for x, y in zip(a, b):\r\n            result |= ord(x) ^ ord(y)\r\n    return result == 0\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8bfb\u4e86\u534a\u5929\u4e5f\u6ca1\u53d1\u73b0\u548c\u666e\u901a\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u6709\u4ec0\u4e48\u4f18\u70b9\uff0c\u76f4\u5230\u770b\u4e86 StackOverflow \u4e0a\u7684\u7b54\u6848\u624d\u77e5\u9053\uff1a\u4e3a\u4e86\u907f\u514d\u653b\u51fb\u8005\u901a\u8fc7\u6d4b\u8bd5\u6bd4\u8f83\u65f6\u95f4\u6765\u5224\u65ad\u6b63\u786e\u7684\u4f4d\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u8ba9\u6bd4\u8f83\u7684\u65f6\u95f4\u6bd4\u8f83\u6052\u5b9a\uff0c\u4e5f\u5c31\u675c\u7edd\u4e86\u8fd9\u79cd\u60c5\u51b5\u3002\uff08\u8bdd\u8bf4\u8fd9\u7b54\u6848\u770b\u5f97\u6211\u5404\u79cd\u4f69\u670d\u554a\uff0c\u641e\u5b89\u5168\u7684\u4e13\u5bb6\u679c\u7136\u4e0d\u662f\u6211\u90a3\u4e48\u80a4\u6d45\u7684\u2026\uff09<\/span><\/p>\n<p><span style=\"font-size: small;\">\u63a5\u7740\u662f\u7ee7\u627f tornado.web.RequestHandler\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u5728\u6267\u884c\u6d41\u7a0b\u4e0a\uff0ctornado.web.Application \u4f1a\u6839\u636e URL \u5bfb\u627e\u4e00\u4e2a\u5339\u914d\u7684 RequestHandler \u7c7b\uff0c\u5e76\u521d\u59cb\u5316\u5b83\u3002\u5b83\u7684 __init__() \u65b9\u6cd5\u4f1a\u8c03\u7528 initialize() \u65b9\u6cd5\uff0c\u6240\u4ee5\u53ea\u8981\u8986\u76d6\u540e\u8005\u5373\u53ef\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u8c03\u7528\u7236\u7c7b\u7684 initialize()\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u63a5\u7740\u6839\u636e\u4e0d\u540c\u7684 HTTP \u65b9\u6cd5\u5bfb\u627e\u8be5 handler \u7684 get\/post() \u7b49\u65b9\u6cd5\uff0c\u5e76\u5728\u6267\u884c\u524d\u8fd0\u884c prepare()\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u4e0d\u4f1a\u4e3b\u52a8\u8c03\u7528\u7236\u7c7b\u7684\uff0c\u56e0\u6b64\u6709\u9700\u8981\u65f6\uff0c\u81ea\u884c\u8c03\u7528\u5427\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u6700\u540e\u4f1a\u8c03\u7528 handler \u7684 finish() \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u6700\u597d\u522b\u8986\u76d6\u3002\u5b83\u4f1a\u8c03\u7528 on_finish() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u88ab\u8986\u76d6\uff0c\u7528\u4e8e\u5904\u7406\u4e00\u4e9b\u5584\u540e\u7684\u4e8b\u60c5\uff08\u4f8b\u5982\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff09\uff0c\u4f46\u4e0d\u80fd\u518d\u5411\u6d4f\u89c8\u5668\u53d1\u9001\u6570\u636e\u4e86\uff08\u56e0\u4e3a HTTP \u54cd\u5e94\u5df2\u53d1\u9001\uff0c\u8fde\u63a5\u4e5f\u53ef\u80fd\u5df2\u88ab\u5173\u95ed\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u987a\u4fbf\u8bf4\u4e0b\u600e\u4e48\u5904\u7406\u9519\u8bef\u9875\u9762\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u7b80\u5355\u6765\u8bf4\uff0c\u6267\u884c RequestHandler \u7684 _execute() \u65b9\u6cd5\uff08\u5185\u90e8\u4f9d\u6b21\u6267\u884c prepare()\u3001get() \u548c finish() \u7b49\u65b9\u6cd5\uff09\u65f6\uff0c\u4efb\u4f55\u672a\u6355\u6349\u7684\u9519\u8bef\u90fd\u4f1a\u88ab\u5b83\u7684 write_error() \u65b9\u6cd5\u6355\u6349\uff0c\u56e0\u6b64\u8986\u76d6\u8fd9\u4e2a\u65b9\u6cd5\u5373\u53ef\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass RequestHandler(tornado.web.RequestHandler):\r\n    def write_error(self, status_code, **kwargs):\r\n        if status_code == 404:\r\n            self.render('404.html')\r\n        elif status_code == 500:\r\n            self.render('500.html')\r\n        else:\r\n            super(RequestHandler, self).write_error(status_code, **kwargs)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8986\u76d6 get_error_html() \u65b9\u6cd5\uff0c\u4e0d\u8fc7\u4e0d\u88ab\u63a8\u8350\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u80fd\u6ca1\u5230 _execute() \u65b9\u6cd5\u5c31\u51fa\u9519\u4e86\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4f8b\u5982 initialize() \u65b9\u6cd5\u629b\u51fa\u4e86\u4e00\u4e2a\u672a\u6355\u6349\u7684\u5f02\u5e38\uff0c\u8fd9\u4e2a\u5f02\u5e38\u4f1a\u88ab IOStream \u6355\u6349\u5230\uff0c\u7136\u540e\u76f4\u63a5\u5173\u95ed\u8fde\u63a5\uff0c\u4e0d\u80fd\u5411\u7528\u6237\u8f93\u51fa\u4efb\u4f55\u9519\u8bef\u9875\u9762\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u518d\u6bd4\u5982\u6ca1\u6709\u627e\u5230\u4e00\u4e2a\u80fd\u5904\u7406\u8be5\u8bf7\u6c42\u7684 handler\uff0c\u5c31\u4f1a\u7528 tornado.web.ErrorHandler \u53bb\u5904\u7406 404 \u9519\u8bef\u3002\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u66ff\u6362\u8fd9\u4e2a\u7c7b\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass PageNotFoundHandler(RequestHandler):\r\n    def get(self):\r\n        raise tornado.web.HTTPError(404)\r\n\r\ntornado.web.ErrorHandler = PageNotFoundHandler\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53e6\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u5728 Application \u7684 handlers \u53c2\u6570\u7684\u6700\u540e\uff0c\u52a0\u4e0a\u4e00\u4e2a\u80fd\u6355\u6349\u4efb\u4f55 URL \u7684 handler\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\napplication = tornado.web.Application(&#x5B;\r\n    # ...\r\n    ('.*', PageNotFoundHandler)\r\n])\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u63a5\u7740\u8bf4\u8bf4\u5904\u7406\u767b\u5f55\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> Tornado \u63d0\u4f9b\u4e86 @tornado.web.authenticated \u8fd9\u4e2a\u88c5\u9970\u5668\uff0c\u5728 handler \u7684 get() \u7b49\u65b9\u6cd5\u524d\u52a0\u4e0a\u5373\u53ef\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u5b83\u4f1a\u4f9d\u8d56\u4e09\u5904\u4ee3\u7801\uff1a<\/span><br \/>\n<span style=\"font-size: small;\"> \u9700\u8981\u5b9a\u4e49 handler \u7684 get_current_user() \u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef get_current_user(self):\r\n    return self.get_secure_cookie('user_id', 0)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5b83\u7684\u8fd4\u56de\u503c\u4e3a\u5047\u65f6\uff0c\u5c31\u4f1a\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\u4e86\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u521b\u5efa application \u65f6\u8bbe\u7f6e login_url \u53c2\u6570\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\napplication = tornado.web.Application(\r\n    &#x5B;\r\n        # ...\r\n    ],\r\n    login_url = '\/login'\r\n)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5b9a\u4e49 handler \u7684 get_login_url() \u65b9\u6cd5\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u5982\u679c\u4e0d\u80fd\u4f7f\u7528\u9ed8\u8ba4\u7684 login_url \u53c2\u6570\uff08\u4f8b\u5982\u666e\u901a\u7528\u6237\u548c\u7ba1\u7406\u5458\u9700\u8981\u4e0d\u540c\u7684\u767b\u5f55\u5730\u5740\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u8986\u76d6 get_login_url() \u65b9\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass AdminHandler(RequestHandler):\r\n    def get_login_url(self):\r\n        return '\/admin\/login'\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u987a\u5e26\u4e00\u63d0\uff0c\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u540e\u65f6\u4f1a\u9644\u5e26\u4e00\u4e2a next \u53c2\u6570\uff0c\u6307\u5411\u767b\u5f55\u524d\u8bbf\u95ee\u7684\u7f51\u5740\u3002\u4e3a\u8fbe\u5230\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u9700\u8981\u5728\u767b\u5f55\u540e\u8df3\u8f6c\u5230\u8be5\u7f51\u5740\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass LoginHandler(RequestHandler):\r\n    def get(self):\r\n        if self.get_current_user():\r\n            self.redirect('\/')\r\n            return\r\n        self.render('login.html')\r\n\r\n    def post(self):\r\n        if self.get_current_user():\r\n            raise tornado.web.HTTPError(403)\r\n        # check username and password\r\n        if success:\r\n            self.redirect(self.get_argument('next', '\/'))\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6b64\u5916\uff0c\u6211\u5f88\u591a\u5730\u65b9\u90fd\u4f7f\u7528\u4e86 AJAX \u6280\u672f\uff0c\u800c\u524d\u7aef\u61d2\u5f97\u53bb\u5904\u7406 403 \u9519\u8bef\uff0c\u6240\u4ee5\u6211\u53ea\u80fd\u6539\u9020\u4e00\u4e0b authenticated() \u4e86\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef authenticated(method):\r\n    &quot;&quot;&quot;Decorate methods with this to require that the user be logged in.&quot;&quot;&quot;\r\n    @functools.wraps(method)\r\n    def wrapper(self, *args, **kwargs):\r\n        if not self.current_user:\r\n            if self.request.headers.get('X-Requested-With') == 'XMLHttpRequest': # jQuery \u7b49\u5e93\u4f1a\u9644\u5e26\u8fd9\u4e2a\u5934\r\n                self.set_header('Content-Type', 'application\/json; charset=UTF-8')\r\n                self.write(json.dumps({'success': False, 'msg': u'\u60a8\u7684\u4f1a\u8bdd\u5df2\u8fc7\u671f\uff0c\u8bf7\u91cd\u65b0\u767b\u5f55\uff01'}))\r\n                return\r\n            if self.request.method in (&quot;GET&quot;, &quot;HEAD&quot;):\r\n                url = self.get_login_url()\r\n                if &quot;?&quot; not in url:\r\n                    if urlparse.urlsplit(url).scheme:\r\n                        # if login url is absolute, make next absolute too\r\n                        next_url = self.request.full_url()\r\n                    else:\r\n                        next_url = self.request.uri\r\n                    url += &quot;?&quot; + urllib.urlencode(dict(next=next_url))\r\n                self.redirect(url)\r\n                return\r\n            raise tornado.web.HTTPError(403)\r\n        return method(self, *args, **kwargs)\r\n    return wrapper\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7136\u540e\u8bf4\u4e0b\u83b7\u53d6\u7528\u6237\u7684 IP \u5730\u5740\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u7b80\u5355\u6765\u8bf4\uff0c\u5728 handler \u7684\u65b9\u6cd5\u91cc\u7528 self.request.remote_ip \u5c31\u80fd\u62ff\u5230\u4e86\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u4e0d\u8fc7\u5982\u679c\u4f7f\u7528\u4e86\u53cd\u5411\u4ee3\u7406\uff0c\u62ff\u5230\u7684\u5c31\u662f\u4ee3\u7406\u7684 IP \u4e86\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u5728\u521b\u5efa HTTPServer \u65f6\u589e\u52a0 xheaders \u7684\u8bbe\u7f6e\u4e86\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nif __name__ == '__main__':\r\n    from tornado.httpserver import HTTPServer\r\n    from tornado.netutil import bind_sockets\r\n\r\n    sockets = bind_sockets(80)\r\n    server = HTTPServer(application, xheaders=True)\r\n    server.add_sockets(sockets)\r\n    tornado.ioloop.IOLoop.instance().start()\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6b64\u5916\uff0c\u6211\u53ea\u9700\u8981\u5904\u7406 IPv4\uff0c\u4f46\u672c\u5730\u6d4b\u8bd5\u65f6\u4f1a\u62ff\u5230 ::1 \u8fd9\u79cd IPv6 \u5730\u5740\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u8bbe\u7f6e\u4e00\u4e0b\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nif settings.IPV4_ONLY:\r\n    import socket\r\n    sockets = bind_sockets(80, family=socket.AF_INET)\r\nelse:\r\n    sockets = bind_sockets(80)\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6700\u540e\u518d\u63d0\u4e0b\u751f\u4ea7\u73af\u5883\u4e0b\u5982\u4f55\u63d0\u9ad8\u6027\u80fd\u3002Tornado \u53ef\u4ee5\u5728 HTTPServer \u8c03\u7528 add_sockets() \u524d\u521b\u5efa\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u5229\u7528\u591a CPU \u7684\u4f18\u52bf\u6765\u5904\u7406\u5e76\u53d1\u8bf7\u6c42\u3002<\/span><br \/>\n<span style=\"font-size: small;\"> \u7b80\u5355\u6765\u8bf4\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nif __name__ == '__main__':\r\n    if settings.IPV4_ONLY:\r\n        import socket\r\n        sockets = bind_sockets(80, family=socket.AF_INET)\r\n    else:\r\n        sockets = bind_sockets(80)\r\n    if not settings.DEBUG_MODE:\r\n        import tornado.process\r\n        tornado.process.fork_processes(0) # 0 \u8868\u793a\u6309 CPU \u6570\u76ee\u521b\u5efa\u76f8\u5e94\u6570\u76ee\u7684\u5b50\u8fdb\u7a0b\r\n    server = HTTPServer(application, xheaders=True)\r\n    server.add_sockets(sockets)\r\n    tornado.ioloop.IOLoop.instance().start()\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6ce8\u610f\u8fd9\u79cd\u65b9\u5f0f\u4e0b\u4e0d\u80fd\u542f\u7528 autoreload \u529f\u80fd\uff08application \u5728\u521b\u5efa\u65f6\uff0cdebug \u53c2\u6570\u4e0d\u80fd\u4e3a\u771f\uff09\u3002<\/span><\/p>\n<p><span style=\"font-size: small; color: #ff0000;\">\u8f6c\u8f7d\u81ea\uff1ahttp:\/\/www.keakon.net\/2012\/12\/03\/Tornado\u4f7f\u7528\u7ecf\u9a8c<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u4e00\u4e2a\u7f51\u7ad9\u7684\u540e\u7aef\u5f00\u53d1\u3002\u56e0\u4e3a\u521d\u671f\u53ea\u6709\u6211\u4e00\u4e2a\u4eba\u505a\uff0c\u6240\u4ee5\u6280\u672f\u9009\u62e9\u4e0a\u5f88\u81ea\u7531\u3002\u5728 web \u670d\u52a1\u5668\u4e0a\u6211\u9009\u62e9\u4e86 Tor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2767","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2767"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2767\/revisions"}],"predecessor-version":[{"id":2768,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2767\/revisions\/2768"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}