By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

WSGI explanation with simple APP

WSGI - Web Server Gateway Interface.

What is WSGI?

WSGI is specified in PEP 3333. The main goal of WSGI is to facilitate easy interconnection of servers and web frameworks/applications. WSGI defines a standerd API for web servers(ex: uWSGI, Twisted, Gunicorn) to connect and talk to web frameworks like Django, flask, pyramid. Server and framework which followed WSGI specs then it enables the use of any framework with any server for example you can use uWSGi, Gunicorn or Twisted with django with little configuraton.


Hello World! programm using WSGI interface:

helloworld_wsgi_app.py

from wsgiref.simple_server import make_server

def helloworld_app(environ, start_response):

    """Simple web application which return Hello World to browser"""

    status = '200 OK'

    response_headers = [('Content-type', 'text/plain')]

    start_response(status, response_headers)

    return [b"Hello World !"]

if __name__ == '__main__':

    # start the serveer

    server = make_server('', 5000, helloworld_app)

    server.serve_forever()
python helloworld_wsgi_app.py

Now open the http://localhost:5000 you can see "Hello World !" text in the browser.

helloworld WSGI App Explaination

The application object must accept two arguments environ and start_response.

environ: The environ parameter is a dictionary object which contains wsgi and other environment variables. some of usefull environ variables or REQUEST_METHOD(The HTTP request method, such as "GET" or "POST"), QUERY_STRING (URL after the "?") etc.

start_response: The start_response parameter is a callable accepting two required positional arguments( status , response_headers). statu is used to specify http status 200, 302, 404, 500 etc. response_headers are used to specify content-type, Cookie, Authorization.

Response: HTTP deals with bytes so we have to return list of byte strings (ex: b"Hello World !").

In above example i have used default WSGI implimentaion of server(make_server) to run helloworld_app.

 

    Posted On
  • 08 June 2017
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Generating PDF Files in Python using xhtml2pdf

There are many ways for generating PDF in python. In this post I will be presenting PDF files generation with xhtml2pdf.

xhtml2pdf: xhtml2pdf is a ...

Continue Reading...
Python to Debian package: Simple, Easy and Fast

Packaging python script to debian follows strict instructions, using the following instructions, most of the steps can be skipped hence making it easy and fast.
Continue Reading...

Python using yield and generators.

Generators are memory efficient. They allow us to code with minimum intermediate arguments, less data structures. Generators are of two types, generator expressions and generator ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties