For a time when I have worked with OpenStack, I stumbled upon for the idea of how the api of most of OpenStack services (Nova, Neutron, Cinder, etc.) implemented? When you type some CLI e.g. nova list, neutron net-list, etc. how do they execute the CLI to talk with API servers? Then I realized that they are implemented with Python wsgi application, webob and Paste. For example, if you check Nova project, you can see the file wsgi.py that defines all of abstractions of wsgi elements such as application, middleware, server, etc. Next, nova-api server will abstract them inside the nova/api/openstack/wsgi.py as an application. The wsgi server of nova is initiated in nova/service.py.

In this article, we will not go further with Paste (actually, it is not difficult to understand, you can easily search on Internet). I just try to create a complete example to show how the wsgi app, middleware, server work. The below result is only for testing, for fun, do not take it in further purposes.

From the wiki:

A WSGI application is passed a Python representation of an HTTP request by an application, and returns content which will normally eventually be rendered by a web browser. A common use for this is when a web server serves content created by Python code.

There are, however, other uses: WSGI middleware is Python code that receives a WSGI request and then performs logic based upon this request, before passing the request on to a WSGI application or more WSGI middleware. WSGI middleware appears to an application as a server, and to the server as an application

 

Here is the source code:

https://github.com/vietstacker/-OpenStack-Testing-Python-WSGI/blob/master/test_wsgi.py

 

When you start the server, go to terminal and type:

curl http://localhost:8080

 

The result will be like this:

etualuo@elxgsgld12-ol:~$ curl http://localhost:8080

From VietStack: Wellcome the 8th Meetup of VietOpenStack

Have FUN and ENJOY the 8th meetup of VietOpenStack

26/2/2016

VietStack Team