There's a friend who let me get into the tornado framework, to be honest, I don't use much of this framework ...
I have some of my own operations and development of relevant examples, share to everyone.
How to install tornado, I think we all understand.
Pip Install tornado
Then say some of his modules, the official website has introduced. I am here again wordy repetition machine, inside mixed clip my understanding.
Main modules
Web-friendfeed uses the underlying WEB framework, which contains most of the important features of Tornado, and you are right to enter anyway.
Escape-xhtml, JSON, URL encoding/decoding method
Database-A simple package for mysqldb, making it easier to use, is an ORM thing.
Template-Python-based Web templating system, similar to JINJA2
HttpClient-Non-blocking HTTP client, which is designed to work with the web and Httpserver, this is similar to adding a urllib2
Auth-Implementation of third-party certifications (including Google Openid/oauth, Facebook Platform, Yahoo Bbauth, FriendFeed Openid/oauth, Twitter OAuth)
Locale-support for localization and translation
Options-command line and profile parsing tools, optimized for server environments, accepting parameters
Bottom module
Httpserver-Implementation of a very simple HTTP server serving the Web module
Iostream-A simple package for non-blocking sockets to facilitate common read and write operations
Ioloop-core I/O loop
Again, the way tornado accepts requests:
About the 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),])
So we visit/niubi/123123123 and we go niubi this class, inside the Get parameter.
About the way to post
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, the general get and post are in one access route, just according to different method to distinguish the corresponding.
The crap is over, everybody 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)
#大家可以写个form测试 can also be tested with curl-d.