cundi / blog

push issues as a blog
2 stars 0 forks source link

WSGI基础[翻译] #24

Open cundi opened 8 years ago

cundi commented 8 years ago

原始链接:http://agiliq.com/blog/2013/07/basics-wsgi/

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.

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.

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.

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:

By now probably you would have read why WSGI came into existence.

Before WSGI, one Python web application would work with some web server and not work with other web server. WSGI was written to fix this. With WSGI, any wsgi compliant web application would work with any wsgi compliant web server.

Python provided make_server() gives a WSGI compliant web server. Other wsgi compliant web servers are Gunicorn, uWSGI etc.

Let's run our web application with gunicorn instead of make_server.

In web_application.py, comment the lines which correspond to make_server. So comment these two lines.

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

#httpd.serve_forever()

Install gunicorn.

pip install gunicorn

Run your web application with gunicorn

gunicorn web_application:application --bind=localhost:8051

You should be able to access http://localhost:8051/.

So it was very easy to switch from one wsgi compliant web server, i.e make_server, to another wsgi compliant web server, i.e gunicorn.

Gunicorn details

You can read more about Gunicorn configurations here.

Switching to another wsgi compliant web server.

Let's now switch from gunicorn to uWSGI. uWSGI is another WSGI compliant web server.

Stop gunicorn using "Ctrl+c"

Install uwsgi.

pip install uwsgi

Use uwsgi to serve your web application.

uwsgi --http :8051 --wsgi-file web_application.py

You should be able to access http://localhost:8051/.

So it was very easy to switch from one wsgi compliant web server, i.e gunicorn, to another wsgi compliant web server, i.e uWSGI.

You should read this to get more idea of WSGI.