Django - Web Server Gateway Interface (WSGI)

web server gateway interface--------web server Gateway interface

It is a common interface standard between web servers and web applications or frameworks defined in the Python language.

The role of WSGI is to convert between protocols. WSGI divides web components into three categories:

- web server
- web middleware
- web application

Here WSGI is a bridge, linking web服务器 and web应用程序.

The WSGI interface definition is very simple. Web developers only need to implement a function to respond to HTTP requests.

hello.py

def application(environ,start_response):
	start_response('200 ok',[('Content-Type','text/html')])
	return ['<h1>hello word!</h>'.encode('utf-8'),]

The application() function above is an HTTP processing function that complies with the WSGI standard. It accepts two parameters:

  • environ:一个包含所有HTTP请求信息的dict对象
  • start_response:一个发送HTTP响应的函数

In the application() function, calling start_response returns the status code and returns a fixed HTTP message body without any other processing.

Then write the WSGI program corresponding to the server program and save it asserver.py
server.py

#从wsgiref模块导入make_server
from wsgiref.simple_server import make_server
#引入服务器端代码
from hello import application
#实例化一个服务器,IP为空,监听端口为8080
httpd = make_server('',8080,application)
print("Serving HTTP on port 8080...")
# 开始监听HTTP请求
httpd.serve_forever()

Here we run the server.py file directly
Insert image description here

Results display:

Please add image description

I hope this article is useful to you!
Thank you for your likes and comments!

Guess you like

Origin blog.csdn.net/qq_44936246/article/details/119958288