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

The relationship between Python web framework and web server

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "the relationship between Python web framework and web server". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We all know that Python, as a powerful language, can adapt to rapid prototyping and the production of larger projects, so it is widely used in the development of web applications.

During the interview, everyone was more or less asked the question: what process has a request gone through from the browser to the data return?

To answer this question, first of all, you need to have a certain amount of underlying knowledge such as HTTP protocol, web framework, server, and so on, as well as some understanding of the relationship between web framework and server. Here is a diagram showing the relationship between them in detail.

Are you a little confused about so many wsgi? It doesn't matter if you don't understand. Next, I will take you step by step to understand the concepts in the picture and clarify the relationship between them.

I. web framework

There are many web frameworks in Python, such as Django, Flask, Tornado, sanic and so on. Some people may not be very familiar with sanic. To say a few words here, its usage is very similar to Flask, but it supports async/await asynchronous and concurrent programming, so its performance is relatively higher. We won't compare these frameworks here, because that's not our focus today!

Familiar friends know that the Web framework contains a series of libraries and a major processor (handler) so that you can build your own code to implement Web applications (such as an interactive website). Most web frameworks contain patterns and tools that do the following:

URL routing

Match the input HTTP request to a specific routing function, and call the corresponding Python code through the routing function

Request and response object

Most of the web frameworks in Python help us encapsulate the HTTP request Request sent by the user, and also provide the Response object sent to the user's browser.

Template engine

The Python code logic that implements the application can be separated from the HTML (or others) that it wants to produce output, such as the better-known Jinja2 template engine. It uses a text-based template language, so it can be used to generate any type of tag, not just HTML. It allows you to customize filters, tags, tests, and global variables

Web service development

In addition to the above, a web framework alone can not support the services on our application, and we need to run the HTTP server on the development machine for rapid development; this requires communication between the web framework and the server and other issues.

Fortunately, Python provides us with such a protocol interface, that is, we often talk about the WSGI protocol! Let's take a look at what WSGI is.

II. WSGI protocol

The Web service gateway interface (WSGI for short) is a standard interface between the Web server and the Python Web application framework. By standardizing the behavior and communication between the Web server and the Python web application framework, WSGI makes it possible to write portable Python web code that can be deployed in any WSGI-compliant web service.

With the WSGI protocol, we don't need to get a set of protocols for every web framework, because basically all Python web frameworks support this protocol.

3. UWSGI server

The uWSGI server is the WSGI container in the picture. Compared with the traditional web server, the stand-alone uWSGI server uses less resources and provides higher performance. The most widely used uWSGI servers in Python mainly include the following:

Gunicorn

Gunicorn (Green Unicorn, Green Unicorn) is a pure Python WSGI server used to support Python applications. Unlike other Python web servers, it has a comprehensive user interface and is very easy to use and configure.

Gunicorn has a reasonable default configuration. However, other servers like uWSGI are relatively customizable and therefore more difficult to use efficiently.

Gunicorn is now the recommended choice for new Python web applications.

Waitress

Waitress is a pure Python WSGI server that claims to have "very efficient performance". Its documentation is not very detailed, but it does provide some good features that Gunicorn does not have (such as HTTP request buffering).

Waitress is becoming increasingly popular in the Python Web development community.

UWSGI

You read it correctly, uWSGI is used to build full-stack hosting services. In addition to process management, process monitoring, and other functions, uWSGI can also act as an application server for a variety of programming languages and protocols-including Python and WSIG. UWSGI can run either as a stand-alone web router or behind a full web server such as Nginx or Apache. For the latter, the web server can configure the operation of uWSGI and applications based on the uwsgi protocol. UWSGI's web server support allows dynamic configuration of Python, passing environment variables, and so on.

IV. Uwsgi protocol

Uwsgi protocol: like WSGI, it is a communication protocol, it is a protocol between web server and uWSGI server, an exclusive protocol of uWSGI server, it is used to define the type of transmission information (type of information). The 4byte before each uwsgi packet is a description of the type of transmission information, and the WSGI protocol is two things.

5. Web server

Web servers include Nginx,Apache,IIS and so on. While excellent web servers can also do load balancing and reverse proxy when receiving http requests.

Nginx

Nginx is a web server and is a reverse proxy for HTTP, SMTP, and other protocols. It is famous for its high performance, relative simplicity and compatibility with many application servers, such as WSGI servers.

It also has convenient features, such as load balancing, basic authentication, streaming, and so on. Nginx is designed to carry a high load of websites and is becoming more and more popular. So we generally use Nginx to do can also do load balancing and reverse proxy!

Of course, in addition to clarifying the above concepts and the relationship between them, you also need to know the knowledge of the HTTP protocol, which is also often asked during the interview, so I won't repeat it here.

That's all for "the relationship between the Python web framework and the web server". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report