Understanding the HTTP protocol and HTML documents, we actually understand that the essence of a Web application is:
The browser sends an HTTP request;
The server receives the request and generates an HTML document;
The server sends the HTML document as the body of the HTTP response to the browser;
The browser receives an HTTP response, removing the HTML document from the HTTP body and displaying it.
Therefore, the simplest Web application is to first save the HTML file, with a ready-made HTTP Server software, receive user requests, read HTML from the file, return. Apache, Nginx, lighttpd, and so on these common static server is doing this thing.
If you want to generate HTML dynamically, you need to implement the above steps yourself. However, accepting HTTP requests, parsing HTTP requests, and sending HTTP responses are all menial jobs, and if we're writing these underlying code ourselves, it's going to take months to read the HTTP specification before we start writing Dynamic HTML.
The correct approach is that the underlying code is implemented by specialized server software, and we use Python to focus on generating HTML documents. Because we don't want to be exposed to TCP connections, HTTP RAW requests, and response formats, we need a unified interface that lets us focus on writing Web services in Python.
This interface is the Wsgi:web Server Gateway Interface.
Wsgi interface definition is very simple, it only requires the web developer to implement a function, you can respond to HTTP requests. Let's look at one of the simplest web versions of "Hello, web!" :
def application (environ, start_response):
start_response (' OK ', [(' Content-type ', ' text/html ')]
Return '
Try The application () function above is an HTTP processing function that conforms to the WSGI standard, which receives two parameters:
Environ: A Dict object that contains all the HTTP request information;
Start_response: A function that sends an HTTP response.
In the application () function, call:
Start_response (' K OK ', [(' Content-type ', ' text/html ')])
Try The header of the HTTP response is sent, note that the header can only be sent once, that is, the start_response () function can only be called once. The Start_response () function receives two parameters, one is an HTTP response code, one is a list of HTTP headers, and each header is represented by a tuple containing two str.
Usually, content-type hair should be sent to the browser. Many other commonly used HTTP headers should also be sent.
Then, the return value of the function '
With Wsgi, we are concerned with how to get the HTTP request information from the Environ Dict object, then construct the HTML, send the header through Start_response (), and return the body.
The entire application () function itself does not involve any part of parsing HTTP, that is, the underlying code does not need to be written by us, we are only responsible for considering how to respond to the request at a higher level.
However, wait, how this application () function is called. If we call ourselves, two parameters environ and start_response are not available, and the returned STR cannot be sent to a browser.
So the application () function must be called by the WSGI server. There are many servers that conform to the WSGI specification, and we can pick one to use. But for now, we just want to test as soon as possible that the application () function we've written can really output HTML to the browser, so find the simplest WSGI server and run our Web application.
The good news is that Python has a built-in Wsgi server called Wsgiref, which is a reference implementation of a WSGI server written in pure python. The so-called "reference implementation" means that the implementation is fully compliant with the WSGI standard, but does not consider any operational efficiency and is intended for development and testing purposes only. running the WSGI service
Let's first write hello.py to implement the WSGI handler for the Web application:
# hello.py
def application (environ, start_response):
start_response (' OK ', [' content-type ', ' text/html ') ])
return '
Try
Then, write a server.py, which is responsible for starting the WSGI server and loading the application () function:
# server.py
# import from Wsgiref module: from
wsgiref.simple_server import make_server
# import the application function we wrote ourselves:
From Hello Import Application
# Creates a server with an empty IP address, a port of 8000, and a processing function of application:
httpd = Make_server (', 8000, application)
print "Serving HTTP on port 8000 ..."
# Start listening for HTTP requests:
Httpd.serve_forever ()
Try
Make sure that the above two files are in the same directory, and then enter the Python server.py on the command line to start the WSGI server:
Note: If Port 8000 is already in use by another program, startup will fail, please modify it to another port.
After successful startup, open the browser, enter Http://localhost:8000/, you can see the results:
At the command line, you can see the wsgiref print log information:
Press CTRL + C to terminate the server.
If you think this Web application is too simple, you can change it a little bit and read the path_info from Environ to show more dynamic content:
# hello.py
def application (environ, start_response):
start_response (' OK ', [' content-type ', ' text/html ') ])
return '
Try
You can enter the user name as part of the URL in the Address bar, which will return Hello, xxx!:
is not a bit of web app feel anymore. Summary
No matter how complex a Web application is, the portal is a WSGI handler function. All input information for the HTTP request can be obtained through environ, and the output of the HTTP response can be start_response () plus the function return value as the body.
Complex Web applications that rely on a WSGI function to handle or are too low, we need to abstract the web framework on top of WSGI to further simplify web development. your support is the greatest motivation for the author's writing.
If you like this tutorial, read and feel the harvest is very large, the expected salary increase of more than 30%, may wish to small sponsorship, let me have the motivation to continue to write high-quality tutorials.
Please click on the button below: