wsgi

Wsgi (Web Server Gateway Interfacfe) is a standard interface for Web servers and Web applications. Any Web Server that implements this interface can work seamlessly with Web applications. Take a look at a graphic point:

As shown in the figure above, WSGi connects to the Web server (HTTP server) on one side and the application on the other. There are many Web frameworks, such as Flask and Webpy, where users can develop a Web application by simply configuring some routing information. The Web framework that follows is equivalent to an application.

So what does this interface look like? Pep333 describes it this way:

(1) The App provides a Callable object that accepts two parameters. The first argument is a dict type that represents the environment variable associated with the request. The second argument is a Callable object that the app calls in the code to set the response header. The app needs to return an iterable sequence of strings representing the message content of the response

(2) After the HTTP request arrives, the Web server invokes the Callable object provided by the App and passes in the parameters of the response. The return value of the App is then sent to the HTTP requestor (such as a browser)

First, let’s look at the most basic example, WSGI’s Hello World!

def hello_world_app(environ, start_response) :
    status = '200 OK' # HTTP Status
    headers = [('Content-type'.'text/plain')] # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return ["Hello World"]
Copy the code

Of course, we need a Web server to test this App, and Python provides a simple web server with a pure Python implementation: wsgiref. This implementation is single-threaded and does not perform very well, but can be used for testing.

def hello_world_app(environ, start_response) :
    status = '200 OK' # HTTP Status
    headers = [('Content-type'.'text/plain')] # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return ["Hello World"]

def main() :
    from wsgiref.simple_server import make_server
    httpd = make_server(' '.8000, hello_world_app)
    print "Serving on port 8000..."

    # Serve until process is killed
    httpd.serve_forever()

if __name__ == '__main__':
    main()
Copy the code

Run this code and type 127.0.0.1:8000 in your browser to see the result

Common Python Web servers include Cherrypy, GEvent-FastCGI, Gunicorn, UWSGi, Twisted, Webpy, Tornado, etc. In practical applications, these web servers may need to be used with other HTTP proxy servers. Such as Nginx.

Common Python Web frameworks include Django, Flask, Webpy, Bottle, Cherrypy, and Tornado. Some Web frameworks also have their own Web servers, such as Cherrypy and Tornado. Tornado also performs well as a Web server because of its asynchronous network library.

Finally, Let’s Build A Web Server. This article includes three parts: the first part introduces HTTP service; The second part implements a WSGI Web server and uses and tests it with various Web frameworks. Part 3 is a concurrency optimization for part 2 implementing the WSGI server.