At present, there are dozens of Python network programming frameworks, and it is obviously unrealistic to learn them one by one. However, these frameworks have a lot in common in the system architecture and operating environment. This article leads readers to learn the common knowledge based on Python network framework development, and the current four mainstream Python network frameworks: Django, Tornado, Flask, Twisted.
Alibaba Cloud Simple Application Server: Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.
Network framework and MVC architecture
The so-called network framework refers to such a set of Python packages, which enables developers to focus on the development of website application business logic without having to deal with the underlying protocols, threads, and processes of network applications. This can greatly improve the work efficiency of developers, while improving the quality of network applications.
Among the dozens of development frameworks in the current Python language, almost all full-stack network frameworks force or guide developers to use the MVC architecture to develop web applications. The so-called full-stack network framework refers to a network framework that provides a series of functions such as HTTP stack, database read and write management, and HTML template engine in addition to encapsulating network and thread operations. The Django, Tornado, and Flask that this article focuses on are typical benchmarks for full-stack web frameworks; and Twisted focuses more on high-performance packaging at the bottom of the web without providing interface functions such as HTML template engines, so it cannot be called a full-stack framework.
The MVC (Model-View-Controller) model was first proposed by Trygve Reenskaug in 1978. It was an internal architecture of the programming language Smalltalk in the 1980s. Later, MVC was borrowed by other languages and became a software architecture model in software engineering. MVC divides the Web application system into three basic parts.
Model (Model): used to encapsulate the data related to the business logic of the application and the processing method of the data. It is the part of the Web application used to process the data logic of the application. Model only provides functional interfaces. Through these The interface can get all the functions of the Model. Model does not depend on View and Controller, they can call Model to access data at any time. Some Models also provide an event notification mechanism to provide real-time data updates for the View or Controller registered on it.
View: Responsible for the display and presentation of data, View is the direct output to the user. A Model in MVC usually provides services for multiple Views. In order to obtain real-time updated data of the Model, the View should be registered in the Model as soon as possible.
Controller: Responsible for collecting user input from the user side, which can be seen as providing the reverse function of View. When the user's input causes the View to change, this change must be reflected to the View through the Model. Under the MVC architecture, Controller generally cannot communicate directly with View, which improves the consistency of business data, that is, using Model as the data center.
These three basic parts are separated from each other, so that there is no need to rewrite business logic and data access code when improving and upgrading the interface and user interaction process. The MVC architecture is shown in Figure 1.
MVC architecture diagram
Note: MVC is also widely used in languages other than Python, such as VC++'s MFC, Java's Structs and Spring, C#'s .NET development framework. Readers should have a deep experience.
4 Python web frameworks: Django, Tornado, Flask, Twisted
Next, learn the 4 mainstream Python web frameworks.
Enterprise-level development framework-Django
Django was born in Kansas (Kansas), USA in 2003. It was originally used to make online news Web sites. In 2005, it joined the BSD license family and became an open source network framework. Django is named after Belgian jazz musician Django Reinhardt. The author's naming of Django means that Django can elegantly play (develop) feature-rich music (web applications).
It is the most prestigious and mature network framework in the current Python world. Originally used to make online news Web sites, it has now developed into the most widely used Python web framework. Django's modules are closely integrated, so it is a relatively closed system while powerful, but its sound online documentation and development community enable developers to find solutions when they encounter problems.
Features of Django framework
Compared with other Web frameworks of Python, Django has the most complete functions. Django defines a complete set of functions for service publishing, route mapping, template programming, and data processing. This also means that Django modules are tightly coupled, and developers need to learn this set of technologies defined by Django itself. The main features of Django are as follows.
The back-end management system is automatically generated: Developers can implement a complete back-end data management Web console with a few simple lines of configuration and code.
The error information is very complete: If there is an abnormal operation during the development and debugging process, Django can provide a very complete error message to help the developer locate the problem, such as the lack of configuration references for the xxx component, so that the developer can immediately correct the error.
High concurrent processing framework-Tornado
Tornado is a powerful and extensible
web server written in Python. It is robust enough to handle high network traffic, but it is lightweight enough to be created and written, and can be used in a large number of applications and tools. Tornado, as the basic framework of the FriendFeed website, was released on September 10, 2009. It has received support from many communities and has been used in a series of different occasions. In addition to FriendFeed and Facebook, many companies have turned to Tornado in production, including Quora, Turntable.fm, http://Bit.ly, Hipmunk, and MyYearbook.
Compared with other Python web frameworks, Tornado has the following characteristics.
Complete Web framework: Like Django, Flask, etc., Tornado also provides necessary tools for developing Web applications such as URL routing mapping, Request context, and template-based page rendering technology.
It is an efficient network library with performance comparable to underlying Python frameworks such as Twisted and Gevent: it provides asynchronous I/O support and timeout event handling. This makes Tornado not only used as a web application server framework, but also used as a crawler application, IoT gateway, game server and other background applications.
Provide efficient HTTPClient: In addition to the server-side framework, Tornado also provides an HTTP client based on the asynchronous framework.
Provide an efficient internal HTTP server: Although other Python web frameworks (Django, Flask) also provide internal HTTP servers, their HTTP servers can only be used in test environments due to performance reasons. The Tornado HTTP server is closely integrated with Tornado asynchronous calls, and can be directly used in the production environment.
Complete WebSocket support: WebSocket is a new standard of HTML5, which realizes two-way real-time communication between browser and server.
Because of the above-mentioned characteristics of Tornado, Tornado is often used as an interface service framework for large-scale sites, instead of focusing on building complete large-scale websites like Django, so this chapter focuses on Tornado's asynchronous and coroutine programming, identity authentication framework, and unique non- WSGI deployment method.
A framework that supports rapid site building-Flask
Flask is a relatively young one in the Python web framework family. It appeared in 2010, which allowed it to absorb the advantages of other frameworks and define its main field in small projects. At the same time, it is extensible. Flask lets developers choose which database plug-in to store their data. Many websites with simple functions but excellent performance are built based on the Flask framework. For example, http://httpbin.org/ is a simple but powerful HTTP test project. Flask is a micro-framework for simple needs and small applications.
Compared with other Python language web frameworks, the characteristics of Flask can be summarized as follows.
Built-in development server and debugger
Network program debugging is the process of testing by hand or compiling the program to correct grammatical and logical errors before putting the compiled website into actual operation. Experienced developers know that this is a necessary step to ensure the official application of the website system.
Flask's own development server eliminates the need for developers to install any other web servers when debugging programs, such as Tomcat, JBoss, Apache, etc. Flask is in a debugging state by default, so that any errors in operation will send information to two targets at the same time: one is the Python Console, which is the console for starting the Python program; the other is the HTTP client, that is, the Flask development server passes the debugging information to Up the client.
Fully compatible with WSGI 1.0 standard
WSGI (Web Server Gateway Interface) is highly scalable and can run in a multi-threaded or multi-process environment. Because of the existence of the Python thread global lock, this feature of WSGI is very important. WSGI is already a major standard in the Python world, and various large web servers have good support for it. WSGI is located between the web application and the web server. It is fully compatible with WSGI so that Flask can be configured to various large web servers.
Based on Unicode encoding
Flask is completely based on Unicode. This is very convenient for making websites with non-pure ASCII character sets. HTTP itself is byte-based, which means that any encoding format can be transmitted in HTTP. However, HTTP requires that the encoding format used in this transmission be explicitly stated in the HTTP Head. By default, Flask will automatically add an HTTP Head in UTF-8 encoding format, so that programmers do not have to worry about encoding issues.
The underlying custom protocol network framework-Twisted
The three Python Web frameworks mentioned above are all around the application layer HTTP, and Twisted is an exception. Twisted is an event-driven network framework written in Python. For applications that pursue server program performance, Twisted framework is a good choice.
Twisted is an open source event-driven framework with a history of more than 10 years. Twisted supports many protocols, including UDP, TCP, TLS at the transport layer, and HTTP and FTP at the application layer. For all these protocols, Twisted provides client and server development tools.
Twisted is a high-performance programming framework. On different operating system platforms, Twisted uses different underlying technologies to achieve high-efficiency communication. In Windows, the implementation of Twisted is based on the I/O Completion Port (IOCP, Input/Output Completion Port) technology, which ensures that the bottom layer efficiently notifies I/O events to the framework and applications; in Linux, the implementation of Twisted is based on epoll technology, epoll is an enhanced version of the multiplexed I/O interface select/poll under Linux. It can significantly improve the system CPU utilization when the program is only a few active in a large number of concurrent connections.
In the development method, Twisted guides programmers to use asynchronous programming model. Twisted provides rich Defer, Threading and other features to support asynchronous programming.