Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of WSGI?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "what is the role of WSGI", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the role of WSGI" bar!

A complete Web application consists of two parts, one is the server program (Server), and the other is the application program (Application). The server program is responsible for receiving the requests sent by the browser, and the application program is responsible for handling the specific business logic. For example, I develop a blog application based on the Django framework, and when deployed in a production environment, I use Gunicorn or Uwsgi as the server program.

There are so many Web frameworks in Python, such as Flask, Django, Bottle, Pyramid, Tornado and so on. Even you can build a Web framework yourself, so why can they all be deployed with Gunicorn or Uwsgi?

The answer is the WSGI specification.

WSGI is not a framework, nor a module, but a specification for interaction between Web applications (Web framework) and Web Server. As long as everyone follows the WSGI specification to implement the Web framework, it can be run using Gunicorn, which was first proposed in PEP333.

WSGI specifies what interfaces the Web server (Server) and the application (Application) need to implement so that they can work together perfectly. This is very similar to the USB Type-C specification. As long as the phone and the charging line follow the Type-C interface specification, everyone's charging line can be reused. The difference is that the charging is fast and slow, while Apple does not follow the Type-C specification. So only his own line can be used as an iPhone.

Now that you almost understand what WSGI is, how does WSGI standardize applications? Quite simply, you only need to write a function in the following format:

Def app (environ, start_response):

Start_response ("200 OK", [("Content-Type", "text/html")])

Return ["Hello world!\ n"]

This function must take two parameters. Environ is a dictionary-like data structure that encapsulates the browser's request data. Start_response is a function that must be called in app, and the argument is the status code and the type of response content. The return value of app is the response data of the request. In the end, app is called by server.

Now we use the http server built into Python to start the program.

From wsgiref.simple_server import make_server

If _ name__ = = "_ _ main__":

Httpd = make_server ("", 8000, app)

Print ("Serving http on port 8000")

Httpd.serve_forever ()

Startup, access address:

Thank you for your reading, the above is the content of "what is the role of WSGI", after the study of this article, I believe you have a deeper understanding of the role of WSGI, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report