Python | WSGI Server Introduction

Introduction to WSGI server

The WSGI interface definition is very simple, it only requires web developers to implement a function to respond to HTTP requests. Let's look at the simplest web version of "Hello, web!":

def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return '

Hello, web!

The above application() function is an HTTP processing function that conforms to the WSGI standard, and it receives two parameters:

environ: a dict object containing all HTTP request information;
start_response: A function that sends an HTTP response.
In the application() function, call:

start_response('200 OK', [('Content-Type', 'text/html')])
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 the HTTP response code, and the other is a set of HTTP Headers represented by a list, and each Header is represented by a tuple containing two strs.

Normally, the Content-Type header should be sent to the browser. Many other commonly used HTTP headers should also be sent.

Then, the return value of the function '

Hello, web!

' will be sent to the browser as the Body of the HTTP response.

With WSGI, what we care about is how to get the HTTP request information from the environ dict object, then construct HTML, send the Header through start_response(), and finally return the Body.

The entire application() function itself does not involve any part of parsing HTTP, that is to say, the underlying code does not need to be written by ourselves, we are only responsible for considering how to respond to requests at a higher level.

How to call the application() function? If we call it ourselves, we cannot provide the two parameters environ and start_response, and the returned str cannot be sent to the 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 choose one to use. But now, we just want to test as soon as possible that the application() function we wrote can really output HTML to the browser, so we must quickly find the simplest WSGI server and run our web application.

Python has a built-in WSGI server. This module is called wsgiref, which is a reference implementation of a WSGI server written in pure Python. The so-called "reference implementation" means that the implementation fully complies with the WSGI standard, but does not consider any operational efficiency, and is only used for development and testing.

from wsgiref.simple_server import make_server

# demo_app requires two parameters
# The 0th parameter indicates the environment (the environment of the computer; the environment related to the request path)
# The first parameter is a function to return the response header
# This function needs a return value, the return value is a list
# There is only one element in the list, which is a binary, representing the data returned to the browser
def demo_app(environ, start_response):
# environ is a dictionary that saves a lot of data
# One of the important ones is that PATH_INFO can get the user's access path
start_response('200 OK', [('Content-Type', 'text/html;charset=utf8')])
return ['hello'.encode('utf8')] # The content displayed by the browser

if __name__ == '__main__':
# demo_app is a function to handle user requests
httpd = make_server('', 8000, demo_app)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")

# The function of the code is to open the browser of the computer and enter http://localhost:8000/xyz?abc in the browser
#import webbrowser

# process a request
# httpd. handle_request()
httpd.serve_forever() # The server runs consistently in the background
Custom WSGI server
from wsgiref.simple_server import make_server

def demo_app(environ, start_response):
# environ is a dictionary that holds a lot of data
# The most important thing is that PATH_INFO can get the user's access path
path = environ['PATH_INFO']
print('path={}'. format(path))
start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ('sss', 'dddd')])
return ['Hello'.encode('utf8')]

if __name__ == '__main__':
httpd = make_server('', 8080, demo_app)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us