We could handle requests sent by web browser in WSGI application either GET or POST requests. We use QUERY_STRING variable for handling GET request, then we use CONTENT_LENGTH and wsgi.input variables for handling POST request. I use python 3.10 for examples.
Handling GET Request
We could use this code as example to handle GET request in WSGI, just copy-paste the code in notepad++, save file as get.py and run on our python environment. I my self use anaconda environment installed in my computer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
###################### #file name: get.py ###################### from wsgiref.simple_server import make_server #from cgi import parse_qs from urllib.parse import parse_qs response_page=''' <html> <head><title>handling GET in WSGI</title></head> <body> <h2>Request GET WSGI</h2> <strong>%s %s</strong>, welcome to WSGI </body> </html> ''' index_page=''' <html> <head><title>handling GET in WSGI</title></head> <body> <h2>Request GET WSGI</h2> <form action="" method="get"> First name:<br/> <input type="text" name="firstname" size="30" /><br/> Last name:<br/> <input type="text" name="lastname" size="30" /><br/> <input type="submit" value="send" /> </form> </body> </html> ''' def application(environ, start_response): if environ['QUERY_STRING']: a=parse_qs(environ['QUERY_STRING']) firstname=a.get('firstname')[0] lastname=a.get('lastname')[0] body= response_page % (firstname,lastname) else: body=index_page status = '200 OK' headers = [ ('Content-Type', 'text/html'), ('Content-Lenght',str(len(body))) ] start_response(status, headers) #return[body] #this line for python2 since wsgi made for python2 return[body.encode()] #this line is for python3 if __name__ == '__main__': server = make_server('localhost', 5000, application) server.serve_forever() |
Open your anaconda terminal, activate the environment, change directory where the get.py file existed, and run command “python get.py”
Next, open your web browser and write on the address bar “localhost:5000”, load the page, input the firstname and lastname, klik the send button and see what happen.
Finally, we’ve succeded to run the code for handling GET request.
The explanations for the code:
Since we handle GET request, so the query string showed on URL is “localhost:5000/?firstname=Danny&lastname=Tan”. The code a=parse_qs(environ[‘QUERY_STRING’]) will convert the query string to the form of Dictionary (dict). Thus the letter “a” is an object of dictionary, so we invoke element from the object a by method get() which we can found in the code “firstname=a.get(‘firstname’)[0]” to invoke the firstname element from object “a”. Since I use python 3, so I change the “code return[body]” into “return[body.encode()]”. It’s because wsgi is made for python 2.
Handling POST Request
Use this code to handle GET request in WSGI. Copy-paste the code in notepad++, save file as post.py and run on your python environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
###################### #file name: post_ok.py ###################### from wsgiref.simple_server import make_server #from cgi import parse_qs from urllib.parse import parse_qs #from cgi import parse_qs, escape response_page=''' <html> <head><title>handling POST in WSGI</title></head> <body> <h2>Request POST WSGI</h2> <strong>%s %s</strong>, welcome to WSGI </body> </html> ''' index_page=''' <html> <head><title>handling POST in WSGI</title></head> <body> <h2>Request POST WSGI</h2> <form action="" method="post"> First name:<br/> <input type="text" name="firstname" size="30" /><br/> Last name:<br/> <input type="text" name="lastname" size="30" /><br/> <input type="submit" value="send" /> </form> </body> </html> ''' def application(environ, start_response): #body=index_page # Get the HTTP method (GET or POST) method = environ['REQUEST_METHOD'] # Get the query parameters for GET requests if method == 'POST': content_length = int(environ.get('CONTENT_LENGTH', 0)) a=parse_qs(environ['wsgi.input'].read(content_length).decode('utf-8')) firstname = a.get('firstname', [''])[0] # Returns the first age value. lastname = a.get('lastname', [''])[0] # Returns the first age value. body= response_page % (firstname,lastname) # For POST requests, read the input data else: #a = {} body=index_page status = '200 OK' headers = [ ('Content-Type', 'text/html'), ('Content-Lenght',str(len(body))) ] start_response(status, headers) #return[body] #this line for python2 since wsgi made for python2 return[body.encode()] #this line is for python3 if __name__ == '__main__': server = make_server('localhost', 5000, application) server.serve_forever() |
Open your anaconda terminal, activate the environment, change directory where the post.py file existed, and run command “python post.py”
Next, open your web browser and write on the address bar “localhost:5000”, load the page, input the firstname and lastname, klik the send button.
Finally, we’ve succeded to run the code for handling POST request.