如何以 UTF-8 编码 WSGI 输出?

发布于 2024-08-19 15:24:59 字数 1210 浏览 3 评论 0原文

我想将编码为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萝莉病 2024-08-26 15:24:59

您需要以列表的形式返回页面:

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; charset=utf-8'),
        ('Content-Length', str(len(output)))
    ])

    return [output]

WSGI 就是这样设计的,这样您就可以生成 HTML(完整的或部分的)。

You need to return the page as a list:

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; charset=utf-8'),
        ('Content-Length', str(len(output)))
    ])

    return [output]

WSGI is designed that way so that you could just yield the HTML (either complete or in parts).

司马昭之心 2024-08-26 15:24:59

AndiDog的答案是正确的,但在某些环境下你必须将应用程序更改为应用程序

def application(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; charset=utf-8'),
        ('Content-Length', str(len(output)))
    ])
    return [output]

AndiDog answer is correct, but in some enviroment you have to change app into application

def application(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; charset=utf-8'),
        ('Content-Length', str(len(output)))
    ])
    return [output]
冷清清 2024-08-26 15:24:59

编辑

vim /usr/lib/python2.7/site.py

encoding = "ascii" # Default value set by _PyUnicode_Init()

encoding = "utf-8"

重启系统

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

vim /usr/lib/python2.7/site.py

encoding = "ascii" # Default value set by _PyUnicode_Init()

to

encoding = "utf-8"

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文