简体   繁体   中英

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?

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM