After reading this, you must understand what WSGI is
python video tutorial column introduces what WSGI is.
I have been writing python web for several years, but I still don’t know what WSGI is and whether there are many people there. This is normal, because as a developer, you rarely need to understand what wsgi is to build a website.
But if you want to write a web framework yourself, you have to understand wsgi.
To review, when we use python for web development, we usually develop it based on a certain web framework, such as django or flask and other frameworks. After the business development is completed, it must be deployed to a server to provide external access.
If you search online at this time, they will tell you that you need to use gunicorn or uwsgi to deploy. So what are gunicorn and uwsgi?
You will understand after looking at this picture. I found the picture from the Internet.
The role played by uwsgi or gunicorn here is the role of the web server. The server here is a software-level server, used to process HTTP requests sent by the browser and return the response results to the front end. The main task of the Web framework is to process business logic and generate results to the web server, and then the web server returns them to the browser.
The communication between the web framework and the web server needs to follow a set of specifications, and this specification is WSGI.
Why should we come up with such a set of regulations? Standards are to unify standards and facilitate everyone's use
Imagine that our mobile phone charging interfaces are now Type-c. Type-c is a standard. Mobile phone manufacturers produce mobile phones and chargers according to this standard. Manufacturers produce chargers according to Type-c specifications, and mobile phones from different manufacturers can be used with chargers from different manufacturers. However, Apple has its own set of regulations, which ultimately results in the Android charger being unable to charge Apple.

How to write an application that complies with the WSGI specification ( What about frameworks) programs and servers?
As shown in the picture above, the left side is the web server, and the right side is the web framework, or application.
Application
WSGI stipulates that the application must be a callable object (the callable object can be a function, a class, or one that implements __call__
instance object), and must accept two parameters, and the return value of the object must be an iterable object.
We can write the simplest example of an application
1 2 3 4 |
|
application is a function, which must be a callable object, and then receives two parameters, the two parameters are: environ and start_response
- environ is a dictionary that stores all content related to the HTTP request, such as headers, request parameters, etc.
- start_response is a function passed by the WSGI server, used to response header, status code is passed to the server.
Calling the start_response function is responsible for passing the response header and status code to the server. The response body is returned to the server by the application function. A complete http response is provided by these two functions.
Any web framework that implements wsgi will have such a callable object
Server
WSGI What the server does is to receive an HTTP request each time and build an environ object , then call the application object, and finally return the HTTP Response to the browser.
The following is the code of a complete wsgi server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
Of course, if you just write a server for development environment, you don’t have to go to so much trouble to reinvent the wheel yourself, because there are built-in modules in Python. Provides wsgi server functionality.
1 2 3 |
|
Only 3 lines of code can provide a wsgi server. Isn’t it super convenient? Finally, let’s visit and test the effect of the browser initiating a request
Above This is an introduction to wsgi. If you have a deep understanding of wsgi, you can familiarize yourself with PEP333
Related free learning recommendations: python video tutorial
The above is the detailed content of After reading this, you must understand what WSGI is. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
