High Performance Web server framework Tornado simple implementation restful interface and development instance _python

Source: Internet
Author: User
Tags oauth openid

A friend asked me to make a tornado frame, to tell the truth, I don't use this framework much ...

I would like to share some of my own research and development related to the example, shared with you.

How to install tornado, I think we all understand.

Pip Install tornado

To say some of his modules, the official website has introduced. Here I repeat the repetition of the machine, which is mixed with my understanding.

Main modules
Web-friendfeed uses the basic WEB framework, which contains most of the important features of Tornado, and you get in anyway.
Escape-xhtml, JSON, URL encoding/decoding method
Database-The simple encapsulation of mysqldb, making it easier to use, is an ORM thing.
Template-a Web Template system based on Python, similar to JINJA2
HttpClient-non-blocking HTTP client, designed to work with web and Httpserver, which is similar to adding a urllib2
Auth-Implementation of Third-party certification (including Google Openid/oauth, Facebook Platform, Yahoo Bbauth, FriendFeed Openid/oauth, Twitter OAuth)
Locale-support for localization and translation
Options-command line and configuration file Resolution tool, optimized for server environment, accepts parameter

Underlying module
Httpserver-the implementation of a very simple HTTP server that serves the Web module
iostream-Simple encapsulation of non-blocking sockets to facilitate common read and write operations
Ioloop-core I/O loops

Again, the way tornado accepts requests:
about how to get

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 when we visit the/niubi/123123123, we go niubi this class, the get parameters inside.
about how to post

Class MainHandler (Tornado.web.RequestHandler): 
  def get (self): 
    self.write ('  
 

In tornado, the general get and post are all in one access route, and are only differentiated according to different method.
It's all over, everybody. Test the 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 () 

#大家可以写个form测试, you can also use curl-d test

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.