Python | Introduction to WSGI Server

Introduction to WSGI Server

The WSGI interface definition is very simple, it only requires web developers to implement a function that can respond to HTTP requests. Let's look at the simplest web version of "Hello, web!": The application() function above is an HTTP handler function that conforms to the WSGI standard and 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, the other is a set of HTTP Header represented by a list, and each Header is represented by a tuple containing two str. Normally, the Content-Type header should be sent to the browser. Many other commonly used HTTP headers should also be sent. . With WSGI, what we care about is how to get the HTTP request information from the environ dict object, then construct the 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, the underlying code does not need to be written by us, 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 pick and 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. And Python has a built-in WSGI server, this module is called wsgiref, which is the reference implementation of the WSGI server written in pure Python. The so-called "reference implementation" means that the implementation fully conforms to the WSGI standard, but does not consider any operational efficiency, and is only used for development and testing. Example: from wsgiref.simple_server import make_server # demo_app requires two parameters # The 0th parameter represents the environment (the environment of the computer; the environment related to the request path) # The first parameter is a function that returns the response header # This function expects 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 holds 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')] # What the browser displays if __name__ == '__main__': # demo_app is a function that handles user requests httpd = make_server('', 8000, demo_app) sa = httpd.socket.getsockname() print("Serving HTTP on", sa[0], "port", sa[1], "...") # The role of the code is to open the computer's browser and enter http://localhost:8000/xyz?abc in the browser # import webbrowser #'http://localhost:8000/xyz?abc') # 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], "...") httpd.serve_forever()

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