简体   繁体   中英

How do I encode WSGI output in UTF-8?

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!

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).

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!

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM