1

I'm confused.

If AppEngine is supposed to allow running of WSGI-employing apps ..

# somewhere in a webapp.RequestHandler
env = dict(os.environ.items())
for key, value in env.items():
    self.response.out.write(key+': '+value+'<br/>')

req_uri = wsgiref.util.request_uri(env)

.. then why does env not contain variables that PEP 333 lists as must-be-present -- causing the wsgiref.util.request_uri() to raise a KeyError?

I'm basically writing some libraries that will need to work either AppEngine or a typical Apache + modwsgi setup. I thought it would be enough to simply write a WSGI compliant app, but seems AppEngine itself .. is not?

maligree
  • 5,939
  • 10
  • 34
  • 51

2 Answers2

4

the environ which must contain wsgi specific keys is the environ passed to the wsgi application callable. PEP-333 does not require that this be the value os.environ. CGI applications will find that many of the keys will be in os.environ, because the gateway server has provided them, and the cgi to wsgi gateway interface (say, wsgiref.handlers.CGIHandler,) need add only the wsgi specific keys before calling the wsgi application.

To be clear, when PEP-333 mentions environ, it does not mean os.environ.

EDIT: google.appengine.ext.webapp.Request apparently inherits from webob.Request. Thus, a webapp handler can access the wsgi environ something like so.

class MainPage(webapp.RequestHandler):
    def get(self):
        dosomethingwith(self.request.environ)
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • And note that when the App Engine Python runtime gets concurrency support, `os.environ` will become particularly untrustworthy, as it could contain data from a request being served by another thread. – Wooble Jul 11 '11 at 16:39
  • as a general rule, be very pedantic about where you get the `environ`, pep-333 makes some choices that enable a high degree of concurrency, wsgi servers may interleave interaction for multiple requests in the same process (multi-threaded) or even in the same thread (asyncronous), although I know of no actual servers in the wild that do the second, and this fact has eluded the designs of many frameworks, which are not async safe in any measure. – SingleNegationElimination Jul 12 '11 at 03:45
1

AFAIK pep 333 says nothing about forcing all of the wsgi environ variables into os.environ unless emulating CGI, only that the wsgi environ variable should contain these things.

Within the context of a wsgi application the environ dictionary is the part that is passed to your wsgi application function. In GAE you can access the wsgi environ dict via request.environ. So I think your code should be more like:

# somewhere in a webapp.RequestHandler
env = self.request.environ
for key, value in env.iteritems():
    self.response.out.write(key+': '+value+'<br/>')
req_uri = wsgiref.util.request_uri(env)
Chris Farmiloe
  • 13,935
  • 5
  • 48
  • 57