The python
environment that this article relies on is:
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.
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!']
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.
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.
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.
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!