Introduction to the web framework of the Python full stack Road series
The essence of all language Web框架
is actually a socket
server, listening to a port, and then running.
Web框架
Consists of two parts, one part is the socket
logical processing of the business, and different processing according to the request
Python is Web框架
divided into two categories,
That is, the socket also contains the business logic processing (tornado)
Does not include sockets (the framework itself implements sockets through third-party modules) only includes business logic processing (DJANGO,FLASK)
wsgi
is web Server Gateway Interface
, translated is the WEB server gateways interface. Specifically, WSGI is a specification that defines how Web servers interact with Python applications so that Web applications written using Python can be interfaced with Web servers. WSGI was first defined in PEP-0333, and the latest version is defined in Python's PEP-3333.
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/91/F5/wKioL1j5tbfS3m_3AAEUN6DYSvI362.png-wh_500x0-wm_ 3-wmp_4-s_514526187.png "title=" 1pythonpep-333 definition. png "width=", "height=" 398 "border=" 0 "hspace=" 0 "vspace=" 0 "style = "WIDTH:700PX;HEIGHT:398PX;" alt= "Wkiol1j5tbfs3m_3aaeun6dysvi362.png-wh_50"/>
The code in the following example RunServer()
is a function of an HTTP handler that conforms to the WSGI standard, which receives two parameters:
-
environ
: A Dict object that contains all the HTTP request information;
-
start_response
: A function that sends an HTTP response;
wsgiref
the module implements a customweb框架
The approximate logic of the code is: two functions definedindex()
Andmanage()
If the URL that the user accesses is127.0.0.1:8000/index
will return
, if the user is accessing the127.0.0.1:8000/manage
will return/manage
, if you visit another page, return404
#!/usr/bin/python2# _*_coding:utf-8 _*_from wsgiref.simple_server import make_ Serverdef index (ARG): # returns a string containing HTML code return " To access the test through the Curl command on this machine
[Email protected]:~$ Curl 127.0.0.1:8000/index
No matter how complex the Web application, the portal is a wsgi the
handler function. All input information for HTTP requests can be via environ
obtained, the output of the HTTP response can be start_response ()
plus the function return value as body, complex web application, the light of a wsgi function to deal with or too low, we need to abstract the web framework above the WSGI to further simplify web development.
MVC and MTVThe MVC pattern is one of the architectural patterns, and the purpose of the MVC pattern is to implement a dynamic programming design that simplifies the subsequent modification and expansion of the program and makes it possible to reuse a part of the program. In addition, this mode makes the programming more intuitive by simplifying the complexity.
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/91/F5/wKiom1j5tdiRc3VPAAAX7GQbjnA071.png-wh_500x0-wm_ 3-wmp_4-s_755778369.png "title=" 2mvc.png "width=" "height=" 397 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width : 600px;height:397px; "alt=" Wkiom1j5tdirc3vpaaax7gqbjna071.png-wh_50 "/>
The software system is separated by the basic parts of itself and also gives the functions of the basic parts. Professionals can be grouped by their own expertise:
控制器Controller
-Responsible for forwarding the request and processing the request;
视图View
-Interface designer for graphic interface design;
模型Model
-Programmers to write the functions that the program should have (real algorithm and so on), database experts for data Management and database design (can be implemented specific functions);
In MVC.M
is RepresentativeMODLE层
,V
RepresentVIEW
Layer, C stands forContrl
Layer.
MTV in theM
is RepresentativeMODLE层
,T
RepresentTemplate(模板层)
,V
RepresentVIEW
Layer.
#Python全栈之路
This article is from the "Eden" blog, reprint please contact the author!
1Python Full Stack Road Series web Framework Introduction