如何以 UTF-8 编码 WSGI 输出?
我想将编码为 UTF-8 的 HTML 页面发送到 Web 浏览器。但是下面的示例失败了:
from wsgiref.simple_server import make_server
def app(environ, start_response):
output = "<html><body><p>Räksmörgås</p></body></html>".encode('utf-8')
start_response('200 OK', [
('Content-Type', 'text/html'),
('Content-Length', str(len(output))),
])
return output
port = 8000
httpd = make_server('', port, app)
print("Serving on", port)
httpd.serve_forever()
这是回溯:
Serving on 8000
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/wsgiref/handlers.py", line 75, in run
self.finish_response()
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/wsgiref/handlers.py", line 116, in finish_response
self.write(data)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/wsgiref/handlers.py", line 202, in write
"write() argument must be a string or bytes"
如果我删除编码并简单地返回 python 3 unicode 字符串,则 wsgiref 服务器似乎会使用浏览器在请求标头中指定的任何字符集进行编码。不过,我希望自己能够进行此控制,因为我怀疑我是否可以期望所有 WSGI 服务器都这样做。我应该怎么做才能返回 UTF-8 编码的 HTML 页面?
谢谢!
I want to send an HTML page to the web browser encoded as UTF-8. However the following example fails:
from wsgiref.simple_server import make_server
def app(environ, start_response):
output = "<html><body><p>Räksmörgås</p></body></html>".encode('utf-8')
start_response('200 OK', [
('Content-Type', 'text/html'),
('Content-Length', str(len(output))),
])
return output
port = 8000
httpd = make_server('', port, app)
print("Serving on", port)
httpd.serve_forever()
Here's the traceback:
Serving on 8000
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/wsgiref/handlers.py", line 75, in run
self.finish_response()
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/wsgiref/handlers.py", line 116, in finish_response
self.write(data)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/wsgiref/handlers.py", line 202, in write
"write() argument must be a string or bytes"
If I remove the encoding and simply return the python 3 unicode string, the wsgiref server seems to encode in whatever charset the browser specifies in the request header. However I'd like to have this control myself as I doubt I can expect all WSGI servers to do the same. What should I do to return a UTF-8 encoded HTML page?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要以列表的形式返回页面:
WSGI 就是这样设计的,这样您就可以
生成
HTML(完整的或部分的)。You need to return the page as a list:
WSGI is designed that way so that you could just
yield
the HTML (either complete or in parts).AndiDog的答案是正确的,但在某些环境下你必须将应用程序更改为应用程序
AndiDog answer is correct, but in some enviroment you have to change app into application
编辑
以
重启系统
para forcar o python 2.7 a trabalhar com utf-8 como padrão pois o mod_wsgibusca a codificacao padrao do python que antesera ascii com no maximo 128 caracteres!
edit
to
reboot system
para forcar o python 2.7 a trabalhar com utf-8 como padrão pois o mod_wsgi busca a codificacao padrao do python que antes era ascii com no maximo 128 caracteres!