Using uWSGI on Python 3.x

The WSGI standard currently does not cover how to write valid python 3.x web application.

The main problem is with how python 3.x manages string objects.

In python 3.x all the strings object (sequences of bytes) are brutally replaced with unicode objects (a sequence of multichar values).

Various solutions have been proposed

 http://mail.python.org/pipermail/web-sig/2009-September/003960.html

We (Unbit) cannot wait the full standardization of the WSGI+Python3.x (our customers are pressing to start developing with python 3.x), so we had to develope an hybrid solution that can be changed (maintaining backward compatibility) after the standardization.

The first string management of uWSGI happens during the protocol parsing. All the (char based) uwsgi protocol vars are all parsed in unicode objects then passed to the wsgi's callable.

The callable returns a series of headers and a body (or a wsgi.file_wrapper call).

The output headers and status line (all in unicode format) are all converted to ASCII (as needed by the HTTP protocol) and spitted out to the webserver.

The body is left untouched, it is sent to the webserver rawly. Idem for wsgi.file_wrapper call.

Sending Unicode data to web-browsers can generate the Armageddon, so you have to force the content-type's charset (UTF-16/UTF-32):

def application(environ, start_response):
	start_response('200 OK', [('Content-Type', 'text/plain; charset=UTF-16')])
	yield 'Hello World' + environ['QUERY_STRING'] + '\n'

This example will output the hello world string and the QUERY_STRING (originally a char sequence) in Unicode.

WARNING this is not a standard implementation, so use it only if you have to.

If WSGI+Python3.x is standardized we will change our code ASAP.