What is the principle of python WSGI Application?

王林
Release: 2023-05-19 13:25:13
forward
1217 people have browsed it

The python environment that this article relies on is:

python WSGI Application原理是什么

What is WSGI

WSGI Also called web server universal gateway interface, the full name is web server gateway interface. It defines a standard for how web servers and web applications should communicate and handle http requests and responses in python , note that it is just a protocol, or a specification or standard. You don’t have to follow this standard, just like the web server we wrote in the previous article.

WSGI is also divided into applications and server gateways. Among them, the well-known Flask belongs to applications, uWSGI, wsgiref Belongs to the server gateway.

Personally, WSGI avoids reinventing the wheel and lets people who are better at doing things. If you write a server, just write a server that conforms to the WSGI specification. , if you are writing on the application side, just write an application that conforms to the WSGI specification. No need to write the wheel repeatedly.

In addition, WSGI can also be connected with traditional web servers, such as: nginx, apache, etc. to interact.

python WSGI Application原理是什么

What WSGI specifies for applications

WSGI specifies that its application must be a An object that can be called, usually, the object is a function or a class. The object must receive 2 parameters, which are the environ dictionary and the start_response function, and the start_response function should be called before the object ends. The function should pass parameters status and headers, where status should define the response status code and phrase, and headers should define the response header.

If it is explained in code, it should be written like this:

def application(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return [b'Hello, world!']
Copy after login

Among them, application is an iterable object that accepts 2 parameters, one Is it environ, its type is a dictionary, or is it start_response, its type is a function, start_response should be called before the end of application function, and pass in the status and headers values.

Modify the code to write a web business layer that complies with WSGI standards

The code we wrote yesterday will return a Hello, Destined Person., let’s take a look, if We use the WSGI standard to write the business layer, how we should define it, and how to call the WSGI server.

python WSGI Application原理是什么

Above, we have introduced a server that conforms to WSGI: wsgiref.simple_server, we only need to follow WSGI standard to write the business layer, which is the above application. When we defined the status code, we probably set it to 299 OK. When setting the request header, we set a Server to pdudo_web_sites.

We use curl to request the server.

python WSGI Application原理是什么

I found that, oh, as long as it is written according to the WSGI specification, you can easily write a simple server.

The above is the detailed content of What is the principle of python WSGI Application?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template