pythongoogle-app-enginewsgiwsgiref

AppEngine confusion - CGI, WSGI-compliant?


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?


Solution

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