Basics of WSGI

By : Akshar Raaj

In this post we will write a web app which will serve few urls. We will not use any Python framework for it. It's only to illustrate how things work under the hood.

Before we start, let's clarify few terms we will be using.

  • Web Server: When we say web server, we mean the software, and not the machine that stores your code. This server recieves the request from a client(Web browser) and returns a response. Web server doesn't create the response, it only returns the response. So, a server needs to talk with a Web Application which can create a response.

  • Web Application: Web Server will get the response from it. It is the job of the Web Application to create a response based on the url and pass this response back to the Web Server. Web Server's job is only to return this response to the client.

  • WSGI: WSGI is an interface. It is just a specification or a set of rules. WSGI is not a software.

WSGI comes into picture because the Web Server needs to communicate with the Web Application. WSGI specifies the rules which needs to be implemented by the Web Application side and the Web Server side so that they can interact with each other. So, a WSGI compliant server will be able to communicate with a WSGI compliant Web Application.

In WSGI architecture, WSGI Application has to be a callable and it needs to be given to the Web Server, so the Web Server can call the Web Application whenever the server recieves a request.

For understanding more on why WSGI came into existence, read about WSGI on wikipedia.

Writing our Web Application. Just copy it for now and we will go through each line to see what exactly is happening.

#web_application.py
from wsgiref.simple_server import make_server

def application(environ, start_response):
    path = environ.get('PATH_INFO')
    if path == '/':
        response_body = "Index"
    else:
        response_body = "Hello"
    status = "200 OK"
    response_headers = [("Content-Length", str(len(response_body)))]
    start_response(status, response_headers)
    return [response_body]

httpd = make_server(
    '127.0.0.1', 8051, application)

httpd.serve_forever()

Run this file python web_application.py. Visit http://127.0.0.1:8051/ and http://127.0.0.1:8051/abcd. You will see first page with response Index and second page with response Hello.

Stepping through the code.

  • Python provides a function called make_server. We can use this function to create a WSGI compliant server provided by Python.
  • We created a callable called application. You can think of this callable as Web Application.
  • make_server() creates an instance of WSGI compliant server. So, httpd is the Web Server in our case.
  • The first and second argument to make_server specifies the host and port on which the server will listen for requests.
  • Third argument to make_server passes the Web Application which the Web Server would use to get the response.
  • In the last line we start the web server using serve_forever.

Whenever a request comes, Web Server running on port 8051 will call the Web Application which in our case is the callable application.

Web Application code details.

  • To interact with WSGI Web Server, Web Application needs to be WSGI compliant as well.
  • Server will call it with two arguments. So, it must accept two arguments. This is one condition for application to be WSGI compliant.
  • First argument passed to it will be a variable containing various information about the request. In our example, we used it to read the request path.
  • Second argument passed to it will be a callable. application must use this callable to notify the server of the status of response and for setting various headers. This is second condition for application to be WSGI compliant.
  • We satisfied both the conditions required for our application to be WSGI compliant.
  • Finally a response is returned by application to the WSGI server.
  • And server finally relays this response back to the client.

Edit:

I overlooked one thing in the application code. It was pointed by defnull on reddit.

The last line of application should not be return response_body. Instead, it should be return [response_body]. Reason being:

  • WSGI server expects the return from application to be an iterable and sends each element of this iterable to the client in an unbufferred fashion.
  • If we keep return response_body, response_body is a string and hence an iterable and so our code still works. But every character of the response_body will be sent one by one.
  • If we keep return [response_body], the iterable is a list here, containing only one element which is the string response_body. So, in this case entire response_body will be sent at once.
  • That's why we should replace return response_body with return [response_body]. Edited the code for this change.

You should read this to get more idea of WSGI.


Related Posts


Can we help you build amazing apps? Contact us today.

Topics : python WSGI

Comments

madhu

Thanks a lot.'recieves' change it

commmenttor
Amreen

Thank you so much for your valuable information.

commmenttor
Make a Strong Statement-Ed Healthy Sunglasses

Basics of WSGI - Agiliq Blog | Django web app development

commmenttor
Outlet Scarpe Uomo On Line 450355

nike air force 1 opinie 404045 Nike Dunk Bebe 435434 air max 90 noir pas cher 511302 twig mac lipstick 114244 Hogan Official Website 533203 Nuovi Modelli Hogan 004012
Outlet Scarpe Uomo On Line 450355 http://www.andreolareti.com/prova/IsabelMarantBobby/Outlet_Scarpe_Uomo_On_Line_450355.asp

commmenttor
nouveau Isabel Marant Heels

The French fashion designer's Parisian headquarters is opposite a Banque de France. In amongst the lavish, establishment fa?ades of the premier arrondissement there are a discreet, dark, industrial-style doorway, which leads into the light, open-plan 230, 000sq ft space that for whatever reason reminds me of the British Library

commmenttor
コーチ

irrrm a sucker for all these コーチ fairly sophisticated nice durible each goes effectively with small trousers we've a brown, torso lover and even chocolate bars i supply any bailey mouse plus gettin the particular bailey key triplet i like コーチ

commmenttor
© Agiliq, 2009-2012