Using selector under wsgiref.CGIHandler
I ran into a problem while trying to use selector (a
very cool little library; one of Luke Arno’s hacks) under wsgiref’s CGIHandler.
Selector assumes environ['PATH_INFO']
is set, but CGIHandler
doesn’t set it, so everything blows up.
Fortunately, Python makes this sort of thing incredibly easy to
work around. First, let’s define a shim class which’ll frob
environ
if necessary:
class SelectorShim:
"""Wraps a Selector so that we can use it under CGIHandler."""
def __init__(self, urls):
self.selector = urls
def __call__(self, environ, start_response):
if 'PATH_INFO' not in environ:
# Either PATH_INFO or SCRIPT_NAME *must* be defined
environ['PATH_INFO'] = environ['SCRIPT_NAME']
return self.selector(environ, start_response)
Using the shim is just a matter of running your selector through it on its way to CGIHandler: just change
CGIHandler().run(urls)
to
CGIHandler().run(SelectorShim(urls))
I love how easy this was to do. I ran into this problem and fixed it while on the bus this morning — I was able to fix it while offline and unable to view any of the relevant documentation. Yet Another reason to love Python.