{"id":3591,"date":"2018-04-17T16:35:57","date_gmt":"2018-04-17T08:35:57","guid":{"rendered":"https:\/\/dmesg.app\/?p=3591"},"modified":"2018-04-22T10:21:56","modified_gmt":"2018-04-22T02:21:56","slug":"python2-3-port","status":"publish","type":"post","link":"https:\/\/dmesg.app\/python2-3-port.html","title":{"rendered":"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898"},"content":{"rendered":"<h2><span style=\"font-size: 12pt;\">\u8d77\u6e90<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u8fd1\u671f\uff0c\u6211\u628a\u4e00\u4e2aPython 2 \u7684\u9879\u76ee\u8fc1\u79fb\u5230\u4e86Python 3\uff0c\u4eca\u65e9\u5728\u8fdb\u884c\u6700\u540e\u4e00\u6b21\u6d4b\u8bd5\u65f6\uff0c\u53d1\u73b0\u4e0b\u8f7d\u7684\u4e00\u4e2a\u6309\u94ae\u662f\u5931\u6548\u7684\u3002\u5f53\u65f6\u7acb\u523b\u5c31\u60f3\uff0c\u5b8c\u86cb\u4e86\uff0c\u96be\u9053\u662f\u9047\u5230\u4e86\u8de8\u5e73\u53f0\u7684\u95ee\u9898\uff1f\u90a3\u5c31\u7cdf\u7cd5\u4e86\u554a\uff0c\u8de8\u5e73\u53f0\u95ee\u9898\u90fd\u4e0d\u592a\u597d\u5904\u7406\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u8bd5\u4e86\u4e0b\u539f\u6765\u7684Python 2\u7248\u672c\u7684\u8fd8\u6b63\u5e38\uff0c\u90a3\u4e48\u5c31\u662f\u8fd9\u6b21\u8fc1\u79fb\u6709\u70b9\u5c0f\u95ee\u9898\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/201804170754535.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<h2><span style=\"font-size: 12pt;\">\u786e\u5b9a\u590d\u73b0<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u7ecf\u8fc7\u4e00\u7cfb\u5217\u7edd\u671b\u7684\u778e\u732b\u78b0\u6b7b\u8017\u5b50\u7684\u5c1d\u8bd5\uff0c\u53d1\u73b0\u4e86\u5f53\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u662f\u7b2c\u4e09\u4e2a\uff0c\u5c31\u4f1a\u89e6\u53d1\u8fd9\u4e2a\u9519\u8bef\uff0c\u629b\u7684\u5f02\u5e38\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"prettyprint linenums\">[E 180417 11:03:48 web:1590] Uncaught exception GET \/apps\/scenario_change\/page\/download?_id=5aab8287e138235c1c9fa9ea&amp;type=task (127.0.0.1)\n    HTTPServerRequest(protocol=&#039;http&#039;, host=&#039;127.0.0.1:9118&#039;, method=&#039;GET&#039;, uri=&#039;\/apps\/scenario_change\/page\/download?_id=5aab8287e138235c1c9fa9ea&amp;type=task&#039;, version=&#039;HTTP\/1.1&#039;, remote_ip=&#039;127.0.0.1&#039;, headers={&#039;Host&#039;: &#039;127.0.0.1:9118&#039;, &#039;Connection&#039;: &#039;keep-alive&#039;, &#039;Upgrade-Insecure-Requests&#039;: &#039;1&#039;, &#039;User-Agent&#039;: &#039;Mozilla\/5.0 (Macintosh; U; Intel Mac OS X 10.11; rv:54.0) Gecko\/20100101 Firefox\/54.0&#039;, &#039;Accept&#039;: &#039;text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8&#039;, &#039;Dnt&#039;: &#039;1&#039;, &#039;Referer&#039;: &#039;http:\/\/127.0.0.1:9118\/apps\/scenario_change\/task_management.html&#039;, &#039;Accept-Encoding&#039;: &#039;gzip, deflate, br&#039;, &#039;Accept-Language&#039;: &#039;zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7&#039;})\n    Traceback (most recent call last):\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 1513, in _execute\n        self.finish()\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 991, in finish\n        self.flush(include_footers=True)\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 947, in flush\n        start_line, self._headers, chunk, callback=callback)\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\http1connection.py&quot;, line 381, in write_headers\n        lines.extend(l.encode(&#039;latin1&#039;) for l in header_lines)\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\http1connection.py&quot;, line 381, in \n        lines.extend(l.encode(&#039;latin1&#039;) for l in header_lines)\n    UnicodeEncodeError: &#039;latin-1&#039; codec can&#039;t encode characters in position 45-46: ordinal not in range(256)\n[E 180417 11:03:48 web:1015] Cannot send error response after headers written\n[E 180417 11:03:48 web:1025] Failed to flush partial response\n    Traceback (most recent call last):\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 1513, in _execute\n        self.finish()\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 991, in finish\n        self.flush(include_footers=True)\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 947, in flush\n        start_line, self._headers, chunk, callback=callback)\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\http1connection.py&quot;, line 381, in write_headers\n        lines.extend(l.encode(&#039;latin1&#039;) for l in header_lines)\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\http1connection.py&quot;, line 381, in \n        lines.extend(l.encode(&#039;latin1&#039;) for l in header_lines)\n    UnicodeEncodeError: &#039;latin-1&#039; codec can&#039;t encode characters in position 45-46: ordinal not in range(256)\n    \n    During handling of the above exception, another exception occurred:\n    \n    Traceback (most recent call last):\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 1022, in send_error\n        self.finish()\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\web.py&quot;, line 992, in finish\n        self.request.finish()\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\httputil.py&quot;, line 419, in finish\n        self.connection.finish()\n      File &quot;C:\\Python36\\lib\\site-packages\\tornado\\http1connection.py&quot;, line 448, in finish\n        self._expected_content_remaining)\n    tornado.httputil.HTTPOutputError: Tried to write 8015 bytes less than Content-Length\n\n<\/pre>\n<h2><span style=\"font-size: 12pt;\">\u8ffd\u8e2a\u5f02\u5e381\uff1a\u767d\u9ad8\u5174<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u6309\u7167\u5e38\u7406\u8bb2\uff0c\u54b1\u4ee3\u7801\u51fa\u4e86\u5f02\u5e38\uff0c\u54b1\u5c31\u5b9a\u4f4d\u5230\u62a5\u9519\u7684\u6700\u540e\u4e00\u884c\uff08most recent call last\u561b\uff0c\u6700\u540e\u4e00\u6b21\u8c03\u7528\uff09\uff0c\u770b\u4e0b\u5f02\u5e38\u662f\u4ec0\u4e48\u77e5\u9053\u4e2a\u5927\u6982\uff0c\u7136\u540e\u8df3\u8fc7\u53bb\uff0c\u8be5\u56de\u6eaf\u7684\u56de\u6eaf\uff0c\u7ba1\u5b83\u662f\u9ad8\u7ea7\u7684\u6253\u65ad\u70b9\u5f00debug\u8fd8\u662f\u4f4e\u7ea7\u7684\u4e00\u987fprint\u7684\uff0c\u786e\u5b9a\u95ee\u9898\u6839\u6e90\u7136\u540e\u4fee\u6539\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u770b\u4e00\u773c\u8fd9\u4e2a\u5f02\u5e38\uff0c<strong>Tried to write 8015 bytes less than Content-Length<\/strong>\uff0c\u770b\u6837\u5b50\u597d\u50cf\u662f\u6307\u5b9a\u4e86\u4e00\u4e2aContent-Length\u4f46\u662f\u5b9e\u9645\u4e0a\u53d1\u9001\u7684\u6587\u4ef6\u5374\u6bd4\u8fd9\u4e2a\u77ed8015\u5b57\u8282\u3002\u83ab\u975e\u662f\u8fc1\u79fb\u52303\u7684\u8fc7\u7a0b\u4e2d\u5bf9Unicode\u4e0ebytes\u5e94\u7528\u4e86\u9519\u8bef\u7684<code>len()<\/code>\u5bfc\u81f4\u629b\u5f02\u5e38\uff1f<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u90a3\u7b80\u5355\u4e86\uff0c\u770b\u4e00\u773c\u662f\u4e0d\u662f\u8bbe\u9519\u4e86headers\u5c31\u597d\u4e86\u554a\uff0cEdit - Find - Find in path\uff0c\u9650\u5b9a\u6587\u4ef6\u7c7b\u578b\u4e3apy\uff0c\u5f00\u641c\uff01<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/201804170754546.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5565\uff1f\u9017\u6211\u5462\uff1f\u6ca1\u6709\uff1f<\/span><br \/>\n<div id=\"sc_tips\"><br \/>\n<span style=\"font-size: 12pt;\">\u5c0f\u63d0\u793a\uff1a\u4e3a\u5565\u7b2c\u4e00\u60f3\u6cd5\u8981\u6000\u7591Content-Length\u7684\u957f\u5ea6\u8bbe\u7f6e\u9519\u4e86\u5462\uff1f<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5f88\u7b80\u5355\u5566\uff0cPython 2\u5bf9Unicode\u7684\u5b9e\u73b0emmm\u8fd9\u4e48\u8bf4\u5427\uff0c\u5728Python 2\u91cc\uff0c<code>'hello'<\/code>\u662f\u4e8c\u8fdb\u5236\uff08\u5b57\u8282\u6d41 bytes\uff09\uff0c<code>u'hello'<\/code>\u624d\u662fUnicode\uff1b\u4f46\u662f\u57283\u91cc\u5462\uff0c<code>'hello'<\/code>\u662fUnicode\uff0c<code>b'hello'<\/code>\u624d\u662f\u4e8c\u8fdb\u5236\u3002\u5509\uff0c\u8c01\u53ebPython \u95ee\u4e16\uff08\u7b2c\u4e00\u7248\u53d1\u5e03\u4e8e1991.02\uff09\u7684\u65f6\u5019Unicode\u6807\u51c6\uff081991.10\uff09\u8fd8\u6ca1\u51fa\u6765\u5462\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4ee5\u9632\u4f60\u4eec\u4e0d\u6653\u5f97\u8fd9\u4e48\u4e2a\u5927\u5751\uff0c\u770b\u4e0b\u56fe\uff1a<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041707545544.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4ee5\u540e\u5728\u6709\u4eba\u8ddf\u4f60\u8bf4\u4e00\u4e2a\u6c49\u5b57\u662f\u4e24\u4e2a\u5b57\u8282\uff0c\u4f60\u5c31\u62ff\u8fd9\u4e2a\u56fe\u603c\u6b7b\u4ed6\uff1a\u4e0d\u8bf4\u7f16\u7801\u5c31\u8bf4\u4e00\u4e2a\u6c49\u5b57\u662f\u51e0\u4e2a\u5b57\u8282\u5c31\u662f\u800d\u6d41\u6c13\uff0c<a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E4%B8%AD%E6%97%A5%E9%9F%93%E7%B5%B1%E4%B8%80%E8%A1%A8%E6%84%8F%E6%96%87%E5%AD%97\">\u8bf4\u4e86\u7f16\u7801\u90a3\u4e5f\u662f\u800d\u6d41\u6c13<\/a>\u3002<\/span><\/div><\/p>\n<h2><span style=\"font-size: 12pt;\">\u8ffd\u8e2a\u5f02\u5e382\uff1a\u627e\u5230\u6839\u6e90<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u6709\u70b9\u626b\u5174\uff0c\u7adf\u7136\u6ca1\u6709Content-Length\uff0c\u90a3\u662f\u56e0\u4e3a\u5565\u5462\uff1f\u90a3\u5c31\u7ee7\u7eed\u770b\u7740\u62a5\u9519\u56de\u6eaf\u5457\u3002\u626b\u4e86\u4e00\u773c\uff0c\u5565\uff0c<span style=\"color: #ff0000;\"><strong>\u90fd\u662ftornado\u7684\u9519<\/strong><\/span>\uff0c<span style=\"color: #339966;\"><strong>\u4e0d\u662f\u6211\u7684\u9519<\/strong><\/span>\uff1f<del>\u96be\u9053\u6211\u8981\u6000\u7591\u7f16\u8bd1\u5668\u89e3\u91ca\u5668\u548c\u4e45\u8d1f\u76db\u540d\u7684tornado\u2026\u2026\uff1f<\/del><\/span><br \/>\n<div id=\"sc_blue\"><br \/>\n<span style=\"font-size: 12pt;\">\u6269\u5c55\u9605\u8bfb\uff1a<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u300a<a href=\"https:\/\/v2mm.tech\/topic\/830\/%E5%A4%A7%E5%AE%B6%E6%9C%89%E4%BB%80%E4%B9%88%E5%86%99%E4%BB%A3%E7%A0%81%E5%86%99%E5%88%B0%E6%80%80%E7%96%91%E4%BA%BA%E7%94%9F-%E6%80%80%E7%96%91%E7%BC%96%E8%AF%91%E5%99%A8%E8%A7%A3%E9%87%8A%E5%99%A8%E7%9A%84%E7%BB%8F%E5%8E%86%E5%90%97\">\u5927\u5bb6\u6709\u4ec0\u4e48\u5199\u4ee3\u7801\u5199\u5230\u6000\u7591\u4eba\u751f\u3001\u6000\u7591\u7f16\u8bd1\u5668\u89e3\u91ca\u5668\u7684\u7ecf\u5386\u5417<\/a>\u300b<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">cookies\u7684\u65f6\u95f4\u6233\uff0cC\u8bed\u8a00\u6807\u51c6for\u5faa\u73af<code>for(int i=0;i++;i&lt;10)<\/code>\uff0cmian\uff0cture\uff0c\u5168\u89d2\u7b26\u53f7\uff0c\u4ee5\u53ca<code>&lt;link href<\/code>......<\/span><\/div><\/p>\n<p><span style=\"font-size: 12pt;\">\u53c8\u4ed4\u7ec6\u770b\u4e86\u4e00\u773c\uff0c\u53d1\u73b0\u8fd9\u4e48\u4e00\u6bb5\u8bdd\uff1a<strong>During handling of the above exception, another exception occurred<\/strong>:<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5904\u7406\u4e0a\u9762\u8fd9\u4e2a\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u53c8\u6765\u4e86\u4e2a\u8fd9\u4e48\u5f02\u5e38\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u554a\u539f\u6765\u6211\u521a\u521a\u770b\u7684\u662f\u540e\u6765\u7684\u5f02\u5e38\u554a\uff0c\u90a3\u6211\u5f97\u770b\u7b2c\u4e00\u4e2a\u5f02\u5e38\u7684\u5c3e\u5df4\uff1a<\/span><\/p>\n<p><strong><span style=\"font-size: 12pt;\">UnicodeEncodeError: 'latin-1' codec can't encode characters in position 45-46: ordinal not in range(256)<\/span><\/strong><\/p>\n<p><span style=\"font-size: 12pt;\">\u591a\u5c11\u5e74\u4e86\uff0c\u65e0\u6570\u7684Python\u7a0b\u5e8f\u5458\u90fd\u66fe\u7ecf\u88abUnicodeEncodeError\u4e0eUnicodeDecodeError\u5751\u7684\u8bf4\u4e0d\u51fa\u8bdd\u6765\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u53ef\u662f\u4f9d\u65e7\u6ca1\u6709\u6211\u7684\u4ee3\u7801\u554a\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u7ec8\u4e8e\uff0c\u901a\u8fc7\u641c\u7d22.txt\uff08\u4e0b\u8f7d\u7684\u6587\u4ef6\u7684\u6269\u5c55\u540d\uff09\u6211\u6210\u529f\u627e\u5230\u4e86\u8fd9\u4e00\u884c\uff1a<\/span><\/p>\n<pre class=\"prettyprint linenums\">file_name = response[&#039;data&#039;][0][&#039;task_name&#039;] + &#039;.txt&#039;\n<\/pre>\n<p><span style=\"font-size: 12pt;\">\u770b\u6765\u662f\u8fd9\u53e5\u8bdd\u8d1f\u8d23\u751f\u6210\u6587\u4ef6\u540d\u7684\uff0c\u627e\u5230\u8fd9\u53e5\u7684\u51fd\u6570\u5b9a\u4e49\uff0c\u7ee7\u7eed\u627e\u8c03\u7528\u8005\uff0c\u53ee\u549a\uff01<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4e00\u770b\u51fd\u6570\u540d\u53ebdownload\uff0c\u770b\u6837\u5b50\u4e0b\u56fe\u8fd9\u91cc\u5c31\u662f\u8be5\u6392\u9519\u7684\u5730\u65b9<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041707545536.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<h2><span style=\"font-size: 12pt;\">\u89e3\u51b3\u5f02\u5e381\uff1a\u778e\u6bd4\u5212<\/span><\/h2>\n<h3><span style=\"font-size: 12pt;\">\u52a0\u4e2aContent-Length<\/span><\/h3>\n<p><span style=\"font-size: 12pt;\">\u5927\u81f4\u770b\u4e86\u773c\uff0c\u597d\u50cf\u6ca1\u5565\u95ee\u9898\u3002\u8981\u4e0d\u5728\u8fd9\u91cc\u52a0\u4e0a\u4e2aContent-Length\u8bd5\u8bd5\uff1f<\/span><\/p>\n<pre class=\"prettyprint linenums\">self.set_header(&quot;Content-Length&quot;, len(file_content.encode(&#039;utf-8&#039;)))\n<\/pre>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041707545689.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u6211\u7684\u57fa\u7840\u8fd8\u662f\u5f88\u7262\u56fa\u7684\uff0c\u6ca1\u7b97\u9519\uff0c\u4e00\u70b9\u95ee\u9898\u90fd\u6ca1\u6709\uff0c\u80af\u5b9a\u6ca1\u9519\u554a\u3002\u4f46\u662f<strong>Tried to write 8015 bytes less than Content-Length<\/strong>\u8fd9\u4e2a\u5f02\u5e38\u8fd8\u662f\u629b\u4e86\u3002<\/span><\/p>\n<h3><span style=\"font-size: 12pt;\">\u6570\u636e\u7c7b\u578b\u9519\u4e86<\/span><\/h3>\n<p><span style=\"font-size: 12pt;\">\u4e5f\u8bb8\u662f\u6570\u636e\u7c7b\u578b\u9519\u4e86\uff1f\u8bf4\u4e0d\u5b9a<code>file_name<\/code>, <code>file_content<\/code>\u9700\u8981\u5b57\u8282\u6d41\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u5462\uff1f\u8fd9\u597d\u50cf\u6709\u70b9\u63a5\u8fd1Unicode\u9519\u8bef\u4e86\u3002\u7b80\u5355\u7684<code>encode('utf-8')<\/code>\u4e00\u4e0b\uff0c\u5f53\u7136\u8fd8\u662f\u7ee7\u7eed\u9519\u5566\u3002<\/span><\/p>\n<h3><span style=\"font-size: 12pt;\">\u4e5f\u8bb8\u662f\u56e0\u4e3aheader\u7684\u4e8b\u60c5<\/span><\/h3>\n<p><span style=\"font-size: 12pt;\">\u90a3\u5c31\u628aheader\u90fd\u6ce8\u91ca\u4e86\u770b\u770b\u3002\u4e8e\u662f\u5728\u6d4f\u89c8\u5668\u91cc\u6253\u5f00\u4e86\u6587\u4ef6\u2026\u2026<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041707545712.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u90a3\u6211\u53ea\u7559Content-Type<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041707545743.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5657(\/\u2267\u25bd\u2266)\/\u6211\u7684\u6587\u4ef6\u540d\u5462\uff0c\u5185\u5bb9\u662f\u6b63\u786e\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u554a\uff0c\u539f\u6765\u662f\u9760\u7684Content-Disposition\u8fd9\u4e2a\u54cd\u5e94\u5934\u6765\u5f3a\u5236\u6d4f\u89c8\u5668\u4e0b\u8f7d\u6587\u4ef6\uff0c\u6307\u5b9a\u6587\u4ef6\u540d\u4ec0\u4e48\u7684\u554a\u2026\u2026<\/span><\/p>\n<h2><span style=\"font-size: 12pt;\">\u89e3\u51b3\u5f02\u5e382\uff1a\u786e\u5b9a\u95ee\u9898<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u7ec8\u4e8e\uff0c\u6211\u786e\u5b9a\u4e86\u51fa\u9519\u7684\u4ee3\u7801\u5728\u8fd9\u4e00\u884c<\/span><\/p>\n<pre class=\"prettyprint linenums\">self.set_header(&quot;Content-Disposition&quot;, &quot;attachment; filename=%s&quot; % file_name)\n<\/pre>\n<p><span style=\"font-size: 12pt;\">\u770b\u6837\u5b50\u662f\u6587\u4ef6\u540d\u3002\u90a3\u5c31\u628afilename\u66ff\u6362\u6210\u786c\u7f16\u7801\u7684<code>test.txt<\/code>\uff0c\u6b63\u5e38\u4e0b\u8f7d\uff1b\u518d\u6362\u6210<code>tes\u554at.txt<\/code>\uff0c\u4e00\u6a21\u4e00\u6837\u7684\u62a5\u9519\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4e8e\u662f\u6211\u679c\u65ad\u7684\u641c\u7d22\u4e86Content-Disposition encoding\u4ee5\u53caContent-Disposition \u7f16\u7801\uff0c\u770b\u5230<a href=\"https:\/\/blog.robotshell.org\/2012\/deal-with-http-header-encoding-for-file-download\/\">\u8fd9\u4e48\u4e00\u7bc7\u8bf4\u5f97\u8fd8\u633a\u6709\u9053\u7406\u7684\u6587\u7ae0<\/a>\uff1a<\/span><\/p>\n<pre class=\"prettyprint linenums\">Content-Disposition: attachment;filename=&quot;$encoded_fname&quot;;\nfilename*=utf-8&#039;&#039;$encoded_fname\n<\/pre>\n<p><span style=\"font-size: 12pt;\">\u5f53\u7136\u4e86\u6211\u6ca1\u770b\u5230\u4e0b\u9762\u90a3\u53e5\u8bdd\uff08<span style=\"color: #ff0000;\"><strong>\u8bfb\u4e66\u8981\u8bfb\u5168\u554a<\/strong><\/span>\uff09\uff0c\u76f4\u63a5\u5957\u4e0a\u4e86<\/span><\/p>\n<pre class=\"prettyprint linenums\">self.set_header(&quot;Content-Disposition&quot;, &quot;attachment; filename*=utf-8&#039;&#039;%s&quot; % file_name)\n<\/pre>\n<p><span style=\"font-size: 12pt;\">\u5f53\u7136\u7ee7\u7eed\u62a5\u9519\u5566\u3002\u8fd8\u662f<code>file_name<\/code>\u6570\u636e\u7c7b\u578b\u4e0d\u5bf9\uff1fencode\u6210UTF-8\uff1f\u5dee\u70b9\u7528\u4e0adecode\uff08\u5b57\u7b26\u4e32\u600e\u4e48\u53ef\u80fd\u518ddecode\u561b\uff1f\u5fc5\u7136\u8981\u62a5\u9519\u7684\uff09<\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/201804170754585.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5657\uff0c\u8fd9\u5012\u771f\u662f<code>tes\u7684t.txt<\/code>\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684UTF-8\u7f16\u7801\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u8fd9\u5c31\u7ea0\u7ed3\u4e86\uff0c\u96be\u9053\u6211\u8981\u628a\u6587\u4ef6\u540d\u641e\u6210\u4ec0\u4e48ASCII\u7684\uff1f\u6bd4\u5982\u5e72\u8106\u662f\u51e0\u4e2a\u771f\u4f2a\u968f\u673a\u6570\u5427\uff1f<\/span><\/p>\n<h2><span style=\"font-size: 12pt;\">\u89e3\u51b3\u5f02\u5e383\uff1a\u89e3\u51b3<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u7136\u540e\u53c8\u627e\u5230\u4e86\u8fd9\u4e48\u4e00\u7bc7<a href=\"http:\/\/lgbolgger.iteye.com\/blog\/2108396\">\u4f8b\u5b50\u4e30\u5bcc\u7684\u6587\u7ae0<\/a>\uff0c\u770b\u5230\u4e00\u534a\u7a81\u7136\u60f3\u8d77\u4e86\uff0c\u65e2\u7136\u8981\u7ed9\u4efb\u610f\u6587\u5b57\u7f16\u7801\u6210ASCII\u5b57\u7b26\u96c6\u5185\u7684\uff0c\u90a3\u5c31\u7528base64\u554a\uff0c\u4f46\u662f\u6d4f\u89c8\u5668\u53c8\u4e0d\u61c2base64\u6ca1\u529e\u6cd5\u7ed9\u8fd8\u539f\u56de\u6765\u554a\u2026\u2026<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5509\uff1f\uff1f\u4e0d\u662f\u6709\u4e2a\u4e1c\u897f\u53ebURL\u7f16\u7801\u5417\uff1f\u8fc5\u901f\u7ffb\u4e86\u7ffb\u4f8b\u5b50\u4e30\u5bcc\u7684\u6587\u7ae0\uff0c\u8fd8\u771f\u63d0\u4e86\u4e00\u53e5\u2026\u2026<\/span><\/p>\n<pre class=\"prettyprint linenums\">from urllib.parse import quote\nself.set_header(&quot;Content-Disposition&quot;, &quot;attachment; filename=%s&quot; % quote(&#039;tes\u7684t.txt&#039;))\n<\/pre>\n<p><span style=\"font-size: 12pt;\"><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041707545941.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" \/><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u6211\u7231emoji\uff0cemoji\u4e5f\u7231\u6211\u3002???\u5178\u578b\u7684\u50b2\u5a07\u53d7\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">??? ??? ??? ??? ??? ??? ???<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5176\u5b9e<a href=\"https:\/\/blog.robotshell.org\/2012\/deal-with-http-header-encoding-for-file-download\/\">\u7b2c\u4e00\u7bc7\u6587\u7ae0<\/a>\u63d0\u4e86\u4e00\u53e5\u7684\uff1a\u5176\u4e2d\uff0c <code>$encoded_fname<\/code>\u6307\u7684\u662f\u5c06 UTF-8 \u7f16\u7801\u7684\u539f\u59cb\u6587\u4ef6\u540d\u6309\u7167 RFC 3986 \u8fdb\u884c\u767e\u5206\u53f7\u7f16\u7801\uff08percent encoding\uff09\u540e\u5f97\u5230\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u767e\u5206\u53f7\u7f16\u7801\u5c31\u662f<a href=\"https:\/\/zh.wikipedia.org\/zh-hans\/%E7%99%BE%E5%88%86%E5%8F%B7%E7%BC%96%E7%A0%81\">URL\u7f16\u7801<\/a>\u5566\u2026\u2026<\/span><\/p>\n<h2><span style=\"font-size: 12pt;\">\u603b\u7ed3<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u8fd9\u4e2a\u5c0f\u95ee\u9898\u5176\u5b9e\u9690\u85cf\u7684\u8fd8\u662f\u5f88\u6df1\u7684\uff0c\u800c\u4e14\u5947\u7279\u7684\u662f\u5728Python 2\u6ca1\u6709\u95ee\u9898\u4f46\u662f\u5230\u4e863\u5c31\u5192\u51fa\u6765\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5149\u770b\u62a5\u9519\u57fa\u672c\u4e0a\u6ca1\u4ec0\u4e48\u7ebf\u7d22\uff0c\u53ea\u80fd\u9760\u5bf9\u4e1a\u52a1\u7684\u719f\u6089\u6478\u7d22\u627e\u5230\u5bf9\u5e94\u7684\u4ee3\u7801\uff0c\u4ed4\u7ec6\u6392\u67e5\u6700\u7ec8\u786e\u5b9a\u5bfc\u81f4\u51fa\u9519\u7684\u4ee3\u7801\u2026\u2026\u7136\u540e\u89e3\u51b3\u4e4b\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u5c31\u6211\u8fd9\u6c34\u5e73\u554a\uff0c\u8fd8\u662f\u522b\u6000\u7591\u7f16\u8bd1\u5668\u89e3\u91ca\u5668\uff0c<del>\u4e5f\u522b\u6000\u7591\u4e45\u8d1f\u76db\u540d\u7684\u5e93\u4e86\uff0c\u7edd\u5bf9\u662f\u6211\u9519\u4e86<\/del>\u3002<\/span><\/p>\n<blockquote><p>You tell me I'm wrong, then you'd better prove you're right. \u2014\u2014M.J.<\/p><\/blockquote>\n<h2><span style=\"font-size: 12pt;\">\u540e\u7eed<\/span><\/h2>\n<p><span style=\"font-size: 12pt;\">\u7ecf\u8fc7\u4e00\u5c0f\u6bb5\u65f6\u95f4\u7684\u9605\u8bfb\u6e90\u4ee3\u7801\uff0c\u5728tornado\u6e90\u4ee3\u7801\u4e2d\u53d1\u73b0\u4e86\u95ee\u9898\u7684\u6839\u6e90\u3002\u8ddf\u7740\u6211\u7684\u8282\u594f\u8d70\uff01<\/span><span style=\"font-size: 12pt;\">\u5148\u770b\u4e0b\u6293\u5305\u7684\u7ed3\u679c\u5427\uff1a<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">Python 2\u672a\u7f16\u7801<\/span><br \/>\n<span style=\"font-size: 12pt;\"><a href=\"https:\/\/dmesg.app\/wp-content\/uploads\/2018\/04\/2018041803535426.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" rel=\"box\" class=\"fancybox\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"148\" class=\"alignnone size-full wp-image-3608\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041803535426.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" alt=\"\" \/><\/a><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">Python 2 URL\u7f16\u7801<\/span><br \/>\n<span style=\"font-size: 12pt;\"><a href=\"https:\/\/dmesg.app\/wp-content\/uploads\/2018\/04\/2018041803535177.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" rel=\"box\" class=\"fancybox\"><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"109\" class=\"alignnone size-full wp-image-3606\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041803535177.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" alt=\"\" \/><\/a><\/span><\/p>\n<p><span style=\"font-size: 12pt;\">Python 3 URL\u7f16\u7801<\/span><br \/>\n<a href=\"https:\/\/dmesg.app\/wp-content\/uploads\/2018\/04\/2018041803535360.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" rel=\"box\" class=\"fancybox\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"154\" class=\"alignnone size-full wp-image-3607\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2018\/04\/2018041803535360.png\" alt=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" title=\"\u4e00\u6b21\u627e\u4e0d\u5230\u9519\u8bef\u7684\u5de8\u5751\u7684http header\u7684url\u7f16\u7801\u7684Python 3\u8fc1\u79fb\u95ee\u9898\" alt=\"\" \/><\/a><\/p>\n<p><span style=\"font-size: 12pt;\">\u6211\u4eec\u77e5\u9053\u95ee\u9898\u662f\u7531set_header\u5f15\u8d77\u7684\uff0c\u6240\u4ee5\u770b\u4e00\u4e0bset_header\u7684\u6e90\u4ee3\u7801\uff1a<\/span><\/p>\n<pre class=\"prettyprint linenums\">def set_header(self, name, value):\n        # type: (str, _HeaderTypes) -&gt; None\n        &quot;&quot;&quot;Sets the given response header name and value.\n\n        If a datetime is given, we automatically format it according to the\n        HTTP specification. If the value is not a string, we convert it to\n        a string. All header values are then encoded as UTF-8.\n        &quot;&quot;&quot;\n        self._headers[name] = self._convert_header_value(value)\n<\/pre>\n<p>\u975e\u5e38\u6e05\u695a\u7684\u770b\u5230\u8c03\u7528\u4e86\u4e00\u4e2a<code>_convert_header_value()<\/code>\uff0c\u7ee7\u7eed\u8ffd\u6eaf<\/p>\n<pre class=\"prettyprint linenums\">    def _convert_header_value(self, value):\n        # type: (_HeaderTypes) -&gt; str\n\n        # Convert the input value to a str. This type check is a bit\n        # subtle: The bytes case only executes on python 3, and the\n        # unicode case only executes on python 2, because the other\n        # cases are covered by the first match for str.\n        if isinstance(value, str):\n            retval = value\n        elif isinstance(value, bytes):  # py3\n            # Non-ascii characters in headers are not well supported,\n            # but if you pass bytes, use latin1 so they pass through as-is.\n            retval = value.decode(&#039;latin1&#039;)\n        elif isinstance(value, unicode_type):  # py2\n            # TODO: This is inconsistent with the use of latin1 above,\n            # but it&#039;s been that way for a long time. Should it change?\n            retval = escape.utf8(value)\n        elif isinstance(value, numbers.Integral):\n            # return immediately since we know the converted value will be safe\n            return str(value)\n        elif isinstance(value, datetime.datetime):\n            return httputil.format_timestamp(value)\n        else:\n            raise TypeError(&quot;Unsupported header value %r&quot; % value)\n        # If \\n is allowed into the header, it is possible to inject\n        # additional headers or split the request.\n        if RequestHandler._INVALID_HEADER_CHAR_RE.search(retval):\n            raise ValueError(&quot;Unsafe header value %r&quot;, retval)\n        return retval\n<\/pre>\n<p><span style=\"font-size: 12pt;\">\u8fd9\u6bb5\u4ee3\u7801\u6709\u70b9\u5197\u957f\uff0c\u4f46\u5b9e\u9645\u4e0a\u53ea\u505a\u4e86\u4e00\u4ef6\u4e8b\u60c5\uff0c\u5c06\u4f20\u9012\u8fc7\u6765\u7684value\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u66f4\u8fdb\u4e00\u6b65\uff0c\u5728Python 2\u4e2d\uff0c\u5982\u679c\u4f20\u8fc7\u6765\u7684\u662f<code>'hello'<\/code>\uff0c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\uff1b\u5982\u679c\u7c7b\u578b\u662f<code>u'hello'<\/code>, \u90a3\u4e48<code>escape.utf8(value)<\/code>\u3002\u5728Python 3\u4e2d\u5982\u679c\u662f<code>'hello'<\/code>\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u662f<code>'hello\u4f60'.encode('utf-8')<\/code>\uff0c\u90a3\u4e48\u7528latin1\u89e3\u7801\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u6700\u7ec8\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4fdd\u8bc1\u8fd4\u56de\u7684\u7c7b\u578b\u662f\u5bf9\u5e94Python 2\/3\u7684str\u7c7b\u578b\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4f46\u5176\u5b9e\u8fd9\u6bb5\u4ee3\u7801\u5e76\u6ca1\u6709\u4ec0\u4e48\u9519\uff0c\u4e0d\u8fc7\u786e\u5b9e\u5199\u7684\u6709\u70b9\u96be\u61c2\u3002<\/span><br \/>\n<span style=\"font-size: 12pt;\">\u4e4b\u540e\u7ee7\u7eed\u8ffd\u6eaf\u5230<code>write_headers()<\/code>\uff0c\u5728http1connection.py\u7684\u7b2c380\u884c\u5de6\u53f3\uff1a<\/span><\/p>\n<pre class=\"prettyprint linenums\">if PY3:\n            lines.extend(l.encode(&#039;latin1&#039;) for l in header_lines)\n        else:\n            lines.extend(header_lines)\n\n<\/pre>\n<p><span style=\"font-size: 12pt;\">\u5176\u4e2dheader_lines\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u5305\u542b\u4e86\u6240\u6709\u7684headers\uff0c\u5176\u7c7b\u578b\u4e3astr\uff08\u5bf9\u5e942\/3\u7684\u7c7b\u578b\uff09<\/span><br \/>\n<span style=\"font-size: 12pt;\">\u90a3\u4e48\u9519\u8bef\u5c31\u663e\u7136\u6613\u89c1\u4e86\uff0c\u5728Python 3\u4e2d\u5bf9<code>'hello'.encode('latin1')<\/code>\u662f\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u7684\uff0c\u4f46\u662f<code>'hello\u548c'.encode('latin1')<\/code>\u80af\u5b9a\u4f1a\u62a5UnicodeEncodeError\u554a\uff0c\u8fd9\u4e5f\u5370\u8bc1\u4e86\u6211\u6700\u5f00\u59cb\u53d1\u73b0\u7684\u7f16\u7801\u9519\u8bef\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4f46\u662f\u5728Python 2\uff0c\u76f4\u63a5\u5c31\u8d70\u4e86\u4e0b\u9762extend\u8fdb\u53bb\u4e86\uff0c\u81ea\u7136\u4e5f\u6ca1\u95ee\u9898\u4e86\u3002<\/span><br \/>\n<span style=\"font-size: 12pt;\">\u4f46\u662f\u5982\u679c\u6211\u4eec\u8c03\u7528set_header\u65f6\u4f20\u9012\u7684\u53c2\u6570\u662f<code>'hell\u4f60'.encode('utf-8')<\/code>\uff0c\u90a3\u4e48\u5728<code>_convert_header_value<\/code>\u4e2d\u4f1a\u88ab\u7528latin1\u89e3\u7801\uff0c\u5728\u8fd9\u91cc\u53c8\u4f7f\u7528latin1\u7f16\u7801\uff0c\u53c8\u56de\u5230\u4e86utf-8\u7f16\u7801\u4e4b\u540e\u7684\u5f62\u6001\uff0c\u6240\u4ee5\u81ea\u7136\u4f1a\u51fa\u73b0\u5bf9\u5e94\u6587\u4ef6\u540d\u7684UTF-8\u7f16\u7801\uff0c\u4e5f\u5370\u8bc1\u4e86\u4e0a\u9762\u4e0b\u8f7d\u51fa\u6765\u7684\u4e71\u7801\u6587\u4ef6\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u6240\u4ee5\uff0c\u5982\u679c\u8981\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\uff0c\u8981\u4e48\u6211\u4eec\u4fdd\u8bc1\u53ea\u4f20\u9012\u8fc7\u6765ASCII\u7684\u6587\u4ef6\u540d\uff08\u8fdb\u884cURL\u7f16\u7801\uff09\uff0c\u8fd9\u6837\u7528latin1\u7f16\u7801\u5c31\u4e0d\u4f1a\u9519\u4e86\u3002\u8981\u4e48\u76f4\u63a5\u628a\u8fd9\u4fe9latin1\u6539\u6210utf-8\u3002<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\u4e0d\u8fc7\u6839\u636e<a href=\"https:\/\/tools.ietf.org\/html\/rfc7230#section-3.2.4\" target=\"_blank\" rel=\"noopener\">RFC7230 section 3.2.4<\/a>:<\/span><\/p>\n<blockquote><p>Historically, HTTP has allowed field content with text in the<br \/>\nISO-8859-1 charset [ISO-8859-1], supporting other charsets only<br \/>\nthrough use of [RFC2047] encoding. In practice, most HTTP header<br \/>\nfield values use only a subset of the US-ASCII charset [USASCII].<br \/>\nNewly defined header fields SHOULD limit their field values to<br \/>\nUS-ASCII octets. A recipient SHOULD treat other octets in field<br \/>\ncontent (obs-text) as opaque data.<\/p><\/blockquote>\n<p><span style=\"font-size: 12pt;\">\u8fd9\u4e48\u505a\u5176\u5b9e\u662f\u6709\u6096\u6807\u51c6\u7684\uff0c\u4f46\u662fPython 2\u7684\u7248\u672c\u76f4\u63a5\u4e0d\u7ba1\u4e0d\u987e\u53d1\u51fa\u53bb\u4e86\u554a\uff0c\u5b9e\u9645\u4e0aPython 2\u7248\u672c\u7684\u5e94\u8be5\u62d2\u7edd\u8fd9\u79cdheaders\u7684\u3002\u6211\u4e5f\u5f88\u7ea0\u7ed3\uff0c\u6211\u4e5f\u89c9\u5f97\u6ca1\u529e\u6cd5\u554a\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8d77\u6e90 \u8fd1\u671f\uff0c\u6211\u628a\u4e00\u4e2aPython 2 \u7684\u9879\u76ee\u8fc1\u79fb\u5230\u4e86Python 3\uff0c\u4eca\u65e9\u5728\u8fdb\u884c\u6700\u540e\u4e00\u6b21\u6d4b\u8bd5\u65f6\uff0c\u53d1\u73b0\u4e0b\u8f7d\u7684\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[],"class_list":["post-3591","post","type-post","status-publish","format-standard","hentry","category-program"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts\/3591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/comments?post=3591"}],"version-history":[{"count":0,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts\/3591\/revisions"}],"wp:attachment":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/media?parent=3591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/categories?post=3591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/tags?post=3591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}