High-Performance Web server Framework Tornado simple implementation of restful interfaces and development examples

Source: Internet
Author: User
Tags oauth openid
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.

  • Related Article

    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.