Tornado source code exploration, tornado source code
I. Start with a simple socket
Run the script and access http: // 127.0.0.1: 8080 in the browser
#!/usr/bin/env python#coding:utf-8 import socket def handle_request(client): buf = client.recv(1024) client.send("HTTP/1.1 200 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()
The above analysis:
1. the browser is actually a socket Client, while the web application is actually a socket server, and the web application has been listening to a port on the server.
2. When a browser requests a web application, you must specify the IP address of the server.(DNS resolution)Establish a socket connection with the port.
3. After a link is established, the web application returns data to the user based on the request.
4. Disconnect the socket connection. (The reason why http is a short link is that after each request is completed, the server will disconnect the socket link)
A Web framework is generally divided into two types, one of which is a framework that contains the above four parts, and the other is a framework that contains only 3rd parts of the functions. Tornado is a framework of the former.
Tornado is a Python-based web framework. Compared with other Web frameworks, Tornado adopts non-blocking methods and epoll applications. This means that Tornado is an ideal Web Framework for real-time Web services.
2. First knowledge of Tornado
You need to install tornado before using tornado. Do not describe how to install tornado. For details, refer to the official website.
Classic hello world case:
import tornado.ioloopimport tornado.web class 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 execute it in sequence:
- Create an Application object and pass a regular expression '/' and the class name MainHandler to the constructor tornado. web. Application (...)
- Execute the listen (...) method of the Application object, that is, application. listen (8888)
- Execute the start () method of the IOLoop class, that is, tornado. ioloop. IOLoop. instance (). start ()
The whole process is actually creating a socket server and listening to port 8888. When the request arrives, it is based on the url and Request Method in the request.(Post, get, put, etc)To specify the method in the corresponding class to process this request. In the above demo, the url is only http: // 127.0.0.1: 8888/index, and the processing class MainHandler is specified. Therefore, if you access http: // 127.0.0.1: 8888/index in a browser, the server returns Hello, world to the browser; otherwise, the server returns 404: Not Found.(Tornado internal defined value)To complete an http request and response.
From the above analysis, we divide the entire Web framework into two parts:
- To be requested, that is, to create a server socket and listen to the port
- Request Processing Stage: when a client is connected, the request is accepted and the corresponding response is made based on the different requests.
To be continued ........................