Bottle (flask)-like tornado Development

Source: Internet
Author: User
ArticleDirectory
    • Cause:
    • Starting from demo:
    • URL ing:
    • Output Response:
    • Asynchronous access:
    • Purpose:
Cause: after the Spring Festival this year, we found tornado web, a collection of app server + Web framework, which happens to have a project that requires the asynchronous remote call feature, I have a lot of feelings about it and its performance is acceptable. But as a Web framework, I don't like its encapsulation method. I think it is not compact and the setting is not clear enough, the use of many functions is a little complicated. When creating a new app, if you do not refer to the existing app as a reference, it is very difficult to re-write it. For example: Code 1
2 Import Tornado. httpserver
3 Import Tornado. ioloop
4 Import Tornado. Options
5 Import Tornado. Web
6
7 From Tornado. Options Import Define, options
8
9 Define ( " Port " , Default = 8888 , Help = " Run on the given port " , Type = INT)
10
11 Class Mainhandler (tornado. Web. requesthandler ):
12 Def Get (Self ):
13 Self. Write ( " Hello, world " )
14
15 Def Main ():
16 Tornado. Options. parse_command_line ()
17 Application = Tornado. Web. Application ([
18 (R " / " , Mainhandler ),
19 ])
20 Http_server = Tornado. httpserver. httpserver (Application)
21 Http_server.listen (options. Port)
22 Tornado. ioloop. ioloop. instance (). Start ()
23
24 If   _ Name __   =   " _ Main __ " :
25 Main ()
26

 

 

It can be seen from the examples on the official website that it is very troublesome to start a service first. In addition, the smart sensing function of Python during development is not very significant, so it is quite tangled in development. In addition, the remote page asynchronous access function that I need most involves n places. Recently, simple APIs such as flask and bottle have made me very popular. However, if wsgi is used to use bottle on the tornado backend, there is no way to use Asynchronous remote access, this is equivalent to caressing a function that I need most, so I am so angry with myself and have implemented it on the basis of Tornado Web framework, because it simplifies tornado development, so we call it easytor from the demo: it is also a helloworld app. We use easytor to do this: easytor has only one file, and you can directly put it in the project directory, or use 1 From Easytor. app Import Apprun, route
2
3 @ Route ( " ^/Hello/([^/] +) $ " )
4 Def Hello (TOR, name ):
5 Return U " Hello % s " % Name
6
7 If   _ Name __ = " _ Main __ " :
8 Apprun ()

 

We import easytor. in the app module, apprun and route are the server startup methods, while route is a decorator used to register a handler function and the mapped URL to the easytor processor list. URL ing: easytor defines many decorator for registering specific methods and attaching them to corresponding events. The most important thing is route. The function registered with route has at least one parameter. The first parameter is the handler object of Tornado. The access parameter, headers, and cookie start with this parameter. For details, see tornado documentation. URL ing is registered using the decorator route. At the same time, the route can also specify whether to authenticate the URL through parameters, whether it is an Ajax request and a method to accept the request. Route can take the group in the regular expression as the parameter of the handler function, as in the demo, If you access/Hello/Alex, it will display Hello Alex OUTPUT response: easytor outputs content to the browser through the return value of the handler function. The return value can be STR, Unicode, or several packaging classes, such as JSON, view, static_file, redirect, remote, and async. Return the instance directly after instantiation. For example, to output JSON, you only need 1 Return JSON (dict (name = " Alex " ))

 

View is used to wrap render output data to the template. Redirect is used for page jumps. Remote is the encapsulation of Remote Asynchronous page retrieval. async is an empty package used to suspend the current request (see tornado Demo: chat for details). asynchronous access is implemented through the remote class, see the following example: 1 @ Route ( " ^/Remoteacc $ " )
2 Def Get_page (TOR ):
3 Return Remote ( " Http: // target .. " , Method = " Get " ,)
4
5 Def On_get_page (TOR, response ):
6 Return Response. Body

 

Comparing Tornado's original example, we can see that the entire API is simple for many purposes: Write lessCode, Less code or less code Project address: Workshop

Next goal: implant the extended interfaces supported by the session and add fsdal to replace the data access module of tornado.

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.