I followed the WSGI Hello World Tutorial , and modified it to learn a little bit about how WSGI URIs are handled. I am familiar with how Apache and PHP deal with requests via the REQUEST_URI parameter, but WSGI uses PATH_INFO instead. That seems odd, but oh well, maybe I’m missing something.
def simple_app(environ, start_response): """Simplest possible application object""" status = '200 OK' response_headers = [('Content-type','text/plain')] start_response(status, response_headers) if environ["PATH_INFO"]=="/blah" : blah = 'blah\n' + environ["PATH_INFO"] if environ["PATH_INFO"]!="/blah" : blah = 'not blah\n' + environ["PATH_INFO"] return [blah] from wsgiref.simple_server import make_server, demo_app httpd = make_server('', 8000, simple_app) print "Serving HTTP on port 8000..."# Respond to requests until process is killed httpd.serve_forever()
This whole WSGI thing seems a lot more edible that the servlet engines that Java uses, but that might be simply due to my reliance on mod_php when I started doing this stuff way back when. For a long time I felt a web server always had to point towards a file, rather than be a running daemon itself. I like the idea of using python to fulfill that role, as it can handle complex objects so well.
WSGI uses a dictionary object to handle the incoming HTTP stuff, like the request method and so on. That dictionary is called the environ.
You can access the values in the dictionary by passing it a key reference, like this:
I’m surprised there isn’t a REQUEST_URI key, but you can create one by concatenating the PATH_INFO + “?” + QUERY_STRING.
I’m also keeping ongoing notes about my learning curve with python here: