Four major Python Web Programming Framework

Four major Python Web Programming Framework

Python network programming framework currently has as many as dozens, one by one they learn is clearly unrealistic. But these frameworks have a lot in common in the system architecture and operating environment, the article takes the reader to learn based on common knowledge of Python web development framework, and currently four kinds of mainstream Python web framework: Django, Tornado, Flask, Twisted.

Network Framework and MVC architecture

The so-called network framework refers to a set of Python package that enables developers to focus on developing business logic of Web applications, and processing aspects of the agreement without the underlying network applications, threads, processes and so on. This can greatly improve developer productivity, while improving the quality of network applications.

In the current development framework for Python dozens of languages, almost all of the full-stack web framework or guide are forced developers to develop Web applications using MVC architecture. The so-called full-stack web framework, in addition to the package refers to the network and thread operations, it also provides HTTP stack, network framework of a series of functions to read and write database management, HTML template engines. The article focuses on Django, Tornado and Flask are typical all-pole network stack frame; Twisted focus more on the high-performance package without the underlying network to provide interface functionality HTML template engines, it can not be called a full stack frame.

MVC (Model-View-the Controller) model was first proposed in 1978 by the Trygve Reenskaug, in the 1980s is an internal architecture of Smalltalk programming language. MVC was later learn other languages, become a software architecture model of software engineering. The MVC Web application system is divided into three basic parts.

Model (the Model) : used to encapsulate business logic and data related to the application and the data processing method, a part of the Web application for data logic processing application program, Model only provides a functional interface through which Interface can get all the features of the Model. Model is not dependent on the View and Controller, they can call the Model to access data at any time. Some Model also provides event notification mechanism, which is registered in the View or Controller provides real-time data updates.

View (View) : is responsible for data display and presentation, View is a direct output to the user. A Model MVC typically serve multiple View. In order to get real-time updates of data Model, View should be registered with the Model early as possible.

The controller (the Controller) : collects user input from a user terminal, it can be seen as a reverse function for the View. When the user's input results View changes, this change must be reflected through the Model to the View. In the MVC architecture, the Controller generally can not communicate directly with a View, which improves the consistency of the service data, i.e. the data center as a Model.

These three basic parts separated from each other, such that in improving and upgrading the user interface and interaction process, does not need to rewrite the business logic and data access code. MVC architecture shown in Figure 1.

MVC architecture diagram

Note: MVC in languages other than Python also has a wide range of applications, such as the VC ++ MFC, Java's Structs and Spring, C # .NET development framework, the reader should have a profound experience.
4 kinds Python network frame: the Django, the Tornado, the Flask, the Twisted

The next four kinds of learning in today's mainstream Python web framework.

Enterprise development framework --Django

Django was born in 2003 in Kansas (Kansas) State, was originally used to make online news Web site, in 2005, joined the family of BSD licenses, to become open source web framework. According to Belgian jazz musician Django Django Reinhardt named authors so named Django Django gracefully means playing rich (development) features music (Web applications).

It is currently the Python world's most prestigious and most sophisticated network framework. Initially used to make online news Web site, it has now developed into the most widely used Python web framework. Combined with relatively close between the modules of Django, so powerful at the same time is a relatively closed system, but its robust online documentation and development community, allowing developers to find solutions when they encounter problems.

The characteristics of the Django framework

Compared to other Python Web framework of, Django's function is the most complete, Django defines the service release, route map, template programming, a complete set of data processing functions. This also means that a tight coupling between modules Django, Django developers need to learn this skill set their own definition. The main features of Django follows.

Well documented: After 10 years of development and improvement, Django has a wide range of applications and improve the online documentation, developers can search the online documentation to find solutions when they encounter problems.
Integrated Data Access Components: Django's own database ORM Model layer components that enable developers to access the database without having to learn other technologies (dbi, SQLAlchemy, etc.).
Powerful URL mapping technique: Django use regular expressions to manage URL mappings, so bring a high degree of flexibility to developers.
Background management system automatically generates: developers through simple configuration and a few lines of code can be achieved complete backend data management Web console.
Error information is complete: If there is abnormal operation, Django can provide a very complete error message development and debugging process to help developers locate problems, such as missing configuration references xxx components, etc., which enables developers to immediately correct the error.

Django's composition structure

Django is following the MVC architecture of Web development framework, which mainly consists of the following components.

Management Tools (Management) : create a set of built-in sites, data migration, maintaining static files command tool.
Model (the Model) : and providing data access interface module, including the definition of the data fields, metadata, data relationships and operation.
View (View) : the Django view encapsulates a series of operations and data flow of the HTTP Request and Response, which includes a main function URL mapping mechanism, binding template and the like.
Template (Template) : is a set of Django template language to render its own page, with a number of built-in way to generate tags and filters define the page.
Forms (Form1) : HTML form generated by the built-in data types and controls.
Management Station (Admin) : Statement by the need to manage the Model, quickly generate background data management website.

High concurrency processing framework --Tornado

Tornado is the use of a powerful and scalable Web server written in Python. It was a strong performance in handling high network traffic enough, but it has enough of a lightweight when creating and writing, and can be used in a large number of applications and tools. Tornado framework as a basis for FriendFeed site, on September 10, 2009 release, has received a lot of community support, and has been applied in a number of different occasions. In addition to FriendFeed and Facebook, there are many companies in the production of steering Tornado, including Quora, Turntable.fm, Bit.ly, Hipmunk and MyYearbook and so on.

Python frame relative to other networks, Tornado has the following characteristics.

Complete Web framework: with Django, Flask and so on, like, Tornado also provides a URL route mapping, Request context, rendering technology, Web application development tools necessary template-based page.
It is an efficient network library, performance and Twisted, Gevent other comparable bottom frame Python: Providing an asynchronous I / O support, a timeout event handling. This addition makes the Tornado server as a Web application framework can also be used to do back-office applications crawler application, things off, the game servers.
Provide efficient HTTPClient: in addition to server-side framework, Tornado further provides an asynchronous HTTP client-based framework.
Provide efficient internal HTTP server : While other Python web frameworks (Django, Flask) also provides an internal HTTP server, but they HTTP server for performance reasons only be used in test environments. The HTTP server Tornado Tornado closely integrated with the asynchronous call can be directly used in the production environment.
Complete WebSocket support: WebSocket HTML5 is a new standard, to achieve real-time two-way communication between the browser and the server.
Because of these characteristics Tornado, Tornado is often used as an interface service framework for a large site, rather than focusing on the establishment of a complete Django as large sites, this chapter focuses on explaining Tornado asynchronous and coroutine programming, authentication framework, the non-unique WSGI deployment.

Support the rapid establishment of framework --Flask

Flask is a Python Web framework of a tribal younger, appeared in 2010, which makes it absorbed the advantages of other frameworks, and to define their main areas on small projects. At the same time, it is extensible, Flask let developers choose to store their data in a database what plug-ins. Many features simple but high performance website is based on Flask framework and structures, such as http://httpbin.org/ is a simple but powerful feature of HTTP performance test project. Flask is a demand for a simple and small micro framework applications.

Web framework relative to other Python language, Flask features can be summarized as follows.

Built-in debugger development server and
network debugging is a good site will be prepared before the actual put into operation and tested by hand or compiler and other methods, correcting grammatical errors and logic errors in the process. Experienced developers know that this is a necessary step to ensure the formal application of the system to the site.
Flask development server that comes with the developers when debugging the program no need to install any other network servers, such as Tomcat, JBoss, Apache and so on. Flask default in debug state, so that any errors will be running at the same time send a message to two objectives: one is the Python Console, that is, start the console Python program; the other is an HTTP client that Flask development server debugging information to the client.
Python unit test functionality and seamless
unit test is a test for the smallest unit of software development, which focuses on the internal structure of the test program, mainly white-box testing methods, the responsibility of the developer. The main objective is to ensure that the unit test function in a given input state, the output can be expected, when the developer does not meet the requirements able to alert personnel.
Flask provided with a built-in unit testing framework unitest Python seamless test interface, i.e. test_client () function Flask object. By test_client () function, a test simulation program may be accessed by a client HTTP handler to call routing Flask, and acquires the output function to verify customized.
Use Jinja2 template
HTML pages and back-office applications linked website has been an important objective of the framework program. Flask solved this problem by using Jinja2 template technology. Jinja2 is a very flexible HTML template technology, which is evolved from the Django template, but more free and more efficient to use than Django template. Jinja2 template using semantic system formulated to provide a flexible template inheritance technology, automatic cross-site attacks and the fight against XSS easy to debug.
WSGI 1.0 is fully compatible with standard
WSGI (Web Server Gateway Interface) are highly scalable and can be run in a multi-threaded or multi-process environment, because there is a global lock Python threads, making this essential characteristic of WSGI. WSGI is already a major industry standard Python, various large network server has its good support. WSGI located between the Web application and the Web server, that is fully compatible with Flask WSGI can be configured into a large variety of network server.
Unicode encoding based on
Flask is fully Unicode-based. This is very convenient for non-production site is pure ASCII character set. HTTP itself is based on bytes, it means that any encoding format can be transmitted in HTTP. However, HTTP requires explicitly stated in this encoding format applied in the transmission of the HTTP Head. By default, Flask HTTP Head will automatically add a UTF-8 encoding format, so that programmers need not worry about coding.

Custom underlying network protocol frame --Twisted

The above mentioned three Python Web frameworks are deployed around the HTTP application layer, while Twisted is an exception. Twisted is an event-driven with a framework written in Python language network, to pursue the application, Twisted framework program performance server is a good choice.

Twisted is a country with 10 years of history of open source event-driven framework. Twisted supports a variety of protocols, including transport layer is UDP, TCP, TLS, and an application layer of HTTP, FTP and the like. For all of these protocols, Twisted provides the client and server side development tools.

Twisted framework of a long history, with all its major release of Python 2, based on the latest version is based on Python 2.7 of Twisted-15.4.0. Twisted community is being developed based on the Python 3 version, but so far still no stable release based on Twisted Python 3.

Twisted is a high-performance programming framework. Caozuojitong on different platforms, Twisted can achieve efficient communication using different underlying technologies. In Windows, Twisted implementation completion port (IOCP, Input / Output Completion Port ) Technology-based I / O, it ensures that the underlying efficiently inform the I / O event to the framework and applications; in Linux, Twisted-based implementation epoll technology, epoll under Linux is multiplexed I / O interface to select / enhanced version of the poll, it can significantly improve CPU utilization program in the case of a large number of concurrent connections in only a few active.
In the development method, Twisted guide programmers asynchronous programming model. Twisted provides a wealth of Defer, Threading and other features to support asynchronous programming.

Guess you like

Origin www.cnblogs.com/an-wen/p/11330834.html