[Web-SIG] WSGI: read method

Ian Bicking ianb at colorstudy.com
Fri Sep 8 20:27:58 CEST 2006

An issue I just realized (as Robert was bringing up these things), is 
that I would like to be able to give input streams that don't have a 
known length.  In particular, I want to be able to do this:

input = environ['wsgi.input']
if hasattr(input, 'json_request'):
     request = input.json_request
     if 'CONTENT_LENGTH' in environ:
         raw_request = input.read(int(environ['CONTENT_LENGTH']))
         raw_request = input.read()
     request = simplejson.loads(raw_request)

The idea is that the request body won't be serialized unless necessary, 
so internal requests (JSON, XMLRPC, etc) can avoid any serialization, 
while the WSGI app can deal with both these cases and normal 
string-based requests.  But I can't set CONTENT_LENGTH during these 
internal requests, because I'd need to figure out how long the 
serialized request body was, and that would require actually serializing it.

In the end it doesn't matter a whole lot, because almost no 
intermediaries every look at wsgi.input, though if WSGI apps expecting a 
JSON request but not aware of .json_request get one of these requests, 
it is likely they will fail.

Hmm... I could also set CONTENT_LENGTH='1', and make .read(1) return the 
actual entire body, totally ignoring the size argument.  Or make it 
'99999', or whatever.  That seems bad-clever, but maybe most workable 
with PEP 333?

Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org

More information about the Web-SIG mailing list