Tornado differs significantly from the current mainstream Web server framework (including most Python frameworks): It is a non-blocking server and is fast. Tornado can process thousands of connections per second based on its non-blocking method and epoll application. This means Tornado is an ideal Web framework for real-time Web services. A friend asked me to engage in the tornado framework. To be honest, I don't use much of this framework...
I will share some of my O & M R & D examples with you.
I want to know how to install tornado.
pip install tornado
Let's talk about some of his modules on the official website. Here I will try again on the rereading machine, which is mixed with my understanding.
Main modules
The basic web framework used by Web-FriendFeed includes most of Tornado's important functions. you can do it right.
Escape-XHTML, JSON, and URL encoding/decoding methods
Database-simple encapsulation of MySQLdb makes it easier to use. it is an orm.
Template-Python-based web template system, similar to jinja2
Httpclient-non-blocking HTTP client, which is designed to work with web and httpserver. This is similar to adding urllib2
Auth-third-party authentication implementation (including Google OpenID/OAuth, Facebook Platform, Yahoo BBAuth, FriendFeed OpenID/OAuth, Twitter OAuth)
Locale-support for localization and translation
Options-the command line and configuration file parsing tools are optimized for the server environment and the parameters are accepted
Underlying module
Httpserver-a simple HTTP server implementation for the web module
Iostream-simple encapsulation of non-blocking socket to facilitate common read/write operations
Ioloop-core I/O loops
Let's talk about how tornado accepts the request:
Get method
class MainHandler(tornado.web.RequestHandler): def get(self): self.write("You requested the main page") class niubi(tornado.web.RequestHandler): def get(self, story_id): self.write("xiaorui.cc niubi'id is " + story_id) application = tornado.web.Application([ (r"/", MainHandler), (r"/niubi/([0-9]+)", niubi), ])
In this way, the access to/niubi/123123123 will go through the niubi class, which contains the get parameter.
Post method
class MainHandler(tornado.web.RequestHandler): def get(self): self.write('') def post(self): self.set_header("Content-Type", "text/plain") self.write("xiaorui.cc and " + self.get_argument("message"))
In tornado, get and post are generally included in an access route, but they are differentiated by different methods.
Let's test get and post.
import tornado.ioloop import tornado.web import json class hello(tornado.web.RequestHandler): def get(self): self.write('Hello,xiaorui.cc') class add(tornado.web.RequestHandler): def post(self): res = Add(json.loads(self.request.body)) self.write(json.dumps(res)) def Add(input): sum = input['num1'] + input['num2'] result = {} result['sum'] = sum return result application = tornado.web.Application([ (r"/", hello), (r"/add", add), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
# You can write a form Test or use curl-d for testing.