This series of blog plans:
1, analysis of the Python-based web framework Tornado source code
2. Develop a sound MVC framework for Python
First will take everyone together to analyze the web framework based on Python writing tornado, in line with easy to read and understand the goal to write this series, sent to let small white can also Zeng understand the truth, not so much as the analysis of the vernacular, Because this series will describe the various points of knowledge in the Web framework in popular language.
A script triggered a "murder" ....
Run the script and access the http://127.0.0.1:8080 on the browser
#!/usr/bin/env python#coding:utf-8import socketdef 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 ', 8080)) Sock.listen (5) while True: connection, address = Sock.accept () handle_request (connection) connection.close () if __name__ = = ' __main__ ': Main ()
Note: For the demo above, we did not analyze the request and did the same with all requests.
The above analysis:
1, the browser is actually a socket client, and the Web application is actually a socket server, and the Web application on the servers have been listening to a port.
2. When the browser requests a web app, you need to specify the IP(DNS resolution) and port of the server to establish a socket connection.
3. After the link is established, the Web application returns the corresponding data to the user according to the request.
4. Disconnect the socket connection. (The reason that HTTP is a short link is because the server disconnects the socket after each request is completed)
For web frameworks, there are generally two categories, one of which is the framework that contains the above 4 parts, and the other is a framework that contains only the 3rd part of the functionality. Tornado is a framework that belongs to the former. Tornado is a Python-based web framework that differs from other web frameworks in its non-blocking approach and its application to Epoll. This means that Tornado is an ideal web framework for real-time Web services.
First knowledge of tornado
With a rudimentary demo showing the flow of Web requests from the browser to the server, the following is a holistic introduction to the Tornado framework from the perspective of God. Need to install before using tornado, for how to install here do not do too much to repeat, see the official website
The classic Hello World case:
Import Tornado.ioloopimport tornado.webclass MainHandler (tornado.web.RequestHandler): def get (self): Self.write ("Hello, World") application = Tornado.web.Application ([ (R "/index", MainHandler),]) if __name__ = = "__ main__ ": application.listen (8888) tornado.ioloop.IOLoop.instance (). Start ()
Run the script, and then click Execute:
- Create a Application object and pass in a regular expression '/' and class name MainHandler to the constructor: Tornado.web.Application (...)
- Executes the listen (...) of the Application object. method, i.e.: Application.listen (8888)
- The start () method that executes the class of the Ioloop class, namely: Tornado.ioloop.IOLoop.instance (). Start ()
The whole process is to create a socket server and listen to port 8888, when the request arrives, according to the URL and request in the request (post, get or put, etc.) to specify the method in the corresponding class to process the request, In the demo above, only the request for URL Http://127.0.0.1:8888/index is specified for processing class MainHandler(see below for details). So, in the browser petition asked: Http://127.0.0.1:8888/index, the server to the browser will return Hello,world, otherwise return 404:not Found(tornado internally defined values), The HTTP request and response is completed once.
from the above analysis, we divide the entire web framework into two parts:
- Preparation phase: Create a server socket and listen for ports
- Process the request phase, that is, when there is a client connection, accept the request, and according to the different requests to make corresponding corresponding
Then in the recorded blog post will follow these two categories to analyze ...
First: Vernacular tornado source code of a script caused by the massacre