Parsing the Request - Get
Run environment.py again and this time call it like this:
http://localhost:8051/?age=10&hobbies;=software&hobbies;=tunning
Notice the QUERY_STRING and the REQUEST_METHOD variables in the environ dictionary. When the request method is GET the form variables will be sent in the URL in the part called query string, that is, everything after the ?. The value of the query string here is age=10&hobbies=software&hobbies=tunning. Notice the hobbies variable appears two times. It can happen when there are checkboxes in the form or when the user type the same variable more than once in the URL.
It is possible to write code to parse the query string and retrieve those values but it is easier to use the CGI module's parse_qs function which returns a dictionary with the values as lists.
Always beware of the user input. Sanitise it to avoid script injection. The CGI's escape function can be used for that.
For the next script to work it must be saved as parsing_get.wsgi as that is the value of the action attribute of the form. The wsgi extension is the most used for the main script when using mod_wsgi.
#!/usr/bin/env python from wsgiref.simple_server import make_server from cgi import parse_qs, escape html = """ <html> <body> <form method="get" action="parsing_get.wsgi"> <p> Age: <input type="text" name="age"> </p> <p> Hobbies: <input name="hobbies" type="checkbox" value="software"> Software <input name="hobbies" type="checkbox" value="tunning"> Auto Tunning </p> <p> <input type="submit" value="Submit"> </p> </form> <p> Age: %s<br> Hobbies: %s </p> </body> </html>""" def application(environ, start_response): # Returns a dictionary containing lists as values. d = parse_qs(environ['QUERY_STRING']) # In this idiom you must issue a list containing a default value. age = d.get('age', [''])[0] # Returns the first age value. hobbies = d.get('hobbies', []) # Returns a list of hobbies. # Always escape user input to avoid script injection age = escape(age) hobbies = [escape(hobby) for hobby in hobbies] response_body = html % (age or 'Empty', ', '.join(hobbies or ['No Hobbies'])) status = '200 OK' # Now content type is text/html response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body] httpd = make_server('localhost', 8051, application) # Now it is serve_forever() in instead of handle_request(). # In Windows you can kill it in the Task Manager (python.exe). # In Linux a Ctrl-C will do it. httpd.serve_forever()