Django 01. Python WEB Framework

Source: Internet
Author: User

Introduction  Python Web framework Essence (socket), Python web Framework Protocol (WSGI), and the use of Python wsgiref module self-developed a web framework.

Web Framework Basics  

The Python Web framework is essentially a socket server, and the user's browser is actually a socket client.


#!/usr/bin/env python
#coding: Utf-8
Import Socket
#客户端
def handle_request (client):
buf = client.recv (1024x768)
client.send ("http/1.1 ok\r\n\r\n")
client.send ("Hello,seven")
#服务器端
def main ():
sock = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
sock.bind (' localhost ', 8000)
Sock.listen (5)
 
While
True:
connection,address = sock.accept ()
handle_request (Connection)
connection.close ()
 
if __name__ = = "__main__":
Main ()
        The above is achieved through the socket, but for the real development of the Python Web program, it is generally divided into two parts: server programs and Applications. The server program is responsible for encapsulating the socket server and collating the various data requested when the request arrives. The application is responsible for the specific logical processing. In order to facilitate the development of the application, there are a lot of web frameworks, such as: django,flask,web.py and so on. Different frameworks have different ways of developing them, but in any case, the applications you develop will have to work with the server program to provide services to the user. In this way, the server program needs to provide different support for different frameworks. This chaotic situation is bad for both the server and the framework. For the server, you need to support a variety of frameworks, for the framework, only the servers that support it can be used by the development of the application. Standardization becomes particularly important at this time. We can set up a standard that is supported by the framework as long as the server program supports this standard, so they can work with it. Once the criteria are determined, both parties are implemented. In this way, the server can support more and more standard frameworks, and the framework can use more servers that support standards. WSGI (Web server Gateway Interface) is a specification that defines the interface format between Web apps and Web servers written in Python, enabling decoupling between Web apps and Web servers.
the standalone WSGI server provided by the Python standard library is called Wsgiref.   #!/usr/bin/env python
#coding: Utf-8
From
wsgiref.simple_server import make_server
def runserver (environ, start_response):
start_response (' K OK ', [(' Content-type ', ' text/html ')])
return '
if __name__ = = ' __main__ ':
httpd = Make_server (", 8000, Runserver)
print "Serving HTTP on port 8000 ..."
Httpd.serve_forever ()
Customizing the web framework  develop a self-made web framework from the Wsgiref module provided by the Python standard library.
 
#!/usr/bin/env python
#coding: Utf-8
From
wsgiref.simple_server import make_server
 
def index ():
return ' index '
 
def login ():
return ' login '
 
def routers ():
    
Urlpatterns = (
('/index/', index),
('/login/', login),
    )
    
return Urlpatterns
 
def runserver (environ, start_response):
start_response (' K OK ', [(' Content-type ', ' text/html ')])
url = environ[' path_info ']
urlpatterns = routers ()
func = None
For
item in Urlpatterns:
if item[0] = = URL:
func = item[1]
Break
if Func:
return func ()
Else:
return ' 404 Not Found '
    
if __name__ = = ' __main__ ':
httpd = Make_server (", 8000, Runserver)
print "Serving HTTP on port 8000 ..."
Httpd.serve_forever ()
After the request comes through the URL filter, by different methods to process, after the logical processing and interaction with the database after the results are fed back to the page.
the difference between MVC and MTV  MVC:Model Database InteractionView page DisplayController Logic ProcessingMTV:
Model Database interaction Template page showing View logic processing
MVC and MTV are just file stacking patterns.

 

Django 01. Python WEB Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.