[docs] [issue11968] wsgiref's wsgi application sample code does not work

Terry J. Reedy report at bugs.python.org
Sat May 7 01:46:57 CEST 2011

Terry J. Reedy <tjreedy at udel.edu> added the comment:

Takayuki is correct, the status and header strings should, it seems, be 'native strings', not bytes.

The experimental proof is to run the current example code (I did so from IDLE editor window on WinXP) and then enter http://localhost:8000/
in a browser. Several error messages appear back in the shell window, like
AssertionError: Header names/values must be of type str (got b'Content-type')
Hit ^C to stop. Delete the b prefixes from the code. Rerun. Reaccess.
And the environment dict appears as promised:
TMP: C:\DOCUME~1\Terry\LOCALS~1\Temp
WATCOM: C:\temp\WatconPermanent
wsgi.multiprocess: False
Takayuki, if you did the same thing, it would have been helpful if you had said so.

Neither version works in 3.1.3. No exception messages either. Firefox says unable to connect. I retried with 3.2.0 and patched example and it worked again.

Running this down in the docs was harder. In the example, function simple_app is a WSGI application object. The status and header objects are passed to the start_response function passed to simple_app after simple_app is passed to make_server. The manual (elsewhere) refers to PEP 3333 for the definition of WSGI app object. The 'start_response Callable' section says the status passed to start_response should be a string and that headers should be a list of tuples of header key and value, but it does not specify the type of the latter. The earlier 'Note on String Types says that headers are, for convenience, native-to-the-version strings while bodies are bytes. So the revised example that works matches the doc.

Phillip or David, can you verify that this is all as intended?

nosy: +pje, r.david.murray, terry.reedy
stage:  -> needs patch
versions: +Python 3.3

Python tracker <report at bugs.python.org>

More information about the docs mailing list