""" Demo of URL handling. From boodebr.appspot.com. May be freely used and modified for any purpose. """ from wsgiref.util import guess_scheme, request_uri, application_uri, \ shift_path_info from cgi import parse_qs def url_demo(environ, start_response): path = environ['PATH_INFO'] if path == '/urldemo/source': status = '200 OK' response_headers = [('Content-type','text/plain')] start_response(status, response_headers) return [open(__file__).read()] status = '200 OK' response_headers = [('Content-type','text/plain')] start_response(status, response_headers) h = '' h += 'PATH_INFO: %s\n' % environ['PATH_INFO'] h += 'QUERY_STRING: %s\n' % environ['QUERY_STRING'] p = parse_qs(environ['QUERY_STRING']) h += 'cgi.parse_qs() yields: %s\n\n' % str(p) h += 'wsgiref.util.guess_scheme(environ): %s\n' % guess_scheme(environ) h += 'wsgiref.util.request_uri(environ): %s\n' % request_uri(environ) h += 'wsgiref.util.application_uri(environ): %s\n\n' % application_uri(environ) h += 'Splitting PATH_INFO into parts with wsgiref.util.shift_path_info(environ):\n' name = shift_path_info(environ) while name is not None: h += ' name: %s\n' % name name = shift_path_info(environ) return [h] # does not know nor care that it is running under AppEngine! from wsgiref.handlers import CGIHandler CGIHandler().run(url_demo)