tornado frame basis 01- Routing Overview

tornado small but large and Django

Tornado Web Web framework is a framework developed by the Python

Web services use Tornado, and can quickly build a high-performance Web services

Tornado is non-blocking, non-blocking asynchronous single-threaded servers, very fast, relatively speaking generally, if the number of simultaneous connections Django about 3000, it was 10,000 Tornado

Easy to learn Tornado learning cost is very low, it is very easy to learn to use Web framework

Tornado installation

pip install tornado

tornado.ioloop Import #-based circulation of epoll.

tornado.web Import #web core module frame

import tornado.httpserver 

from tornado.options import define,options

define('port',default = 8080,help = 'run port', type = int)

class MainHandler(tornado.web.RequestHandler):

  def get(self):

    self.write('hello this is my first WebWerver')


class IndexHandler():

  

  application = tornado.web.Application(

  [

    (R & lt '/', MainHandler), # data is stored tuple.

    ('/index',IndexHandler)

    # Used to store path

]

  ) # Instantiated core module based on the following classes, interfaces the core application server .web framework calls.

if __name__ = '__main__':

  http_server = tornado.httpserver.HTTPServer(application)  

  http_server.listen (options.port) # bind operation

  tornado.ioloop.IOloop.current (). Start () #, ask a cycle of epoll. Turn on the server to access different paths, return to different resources.

 

routing

get_arguments() ?key=value

Simple example

import tornado.ioloop

import tornado.web

MainHandler class (tornado.web.RequestHandler): # Inheritance bottom package contents.

    def get(self):

        self.write("hello,this is my first webserver")

 

application = tornado.web.Application ([ core applications #

    (R & lt "/", MainHandler), # Handler and define a path

])

 

if __name__ == "__main__":

       application.listen(8080)

  tornado.ioloop.IOLoop.current().start()

  tornado.ioloop #开启循环,让服务一直等待请求的到来

  tornado.web #web框架核心模块,服务基本功能都封装在此模块中

路由表

application = tornado.web.Application([

    (r"/",MainHandler),

])

客户端访问服务器可以看成是:客户端读取服务器资源的一个过程,路由表就指定了具体访问什么资源

在 Tornado 中,路由表通过如上方式定义

路由表是访问服务器的入口,在工作中如果有新的需求,往往只需要在路由表中添加新的路由即可

Handler

class MainHandler(tornado.web.RequestHandler):

    def get(self):

        self.write("hello ")

在这里指定请求的资源 ,

启动Tornado

导入

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

Handler

class IndexHandler(tornado.web.RequestHandler):

    def get(self):

        self.write('hello')

路由

if __name__ == '__main__':

    tornado.options.parse_command_line() #打印请求信息.

    app = tornado.web.Application(

        handlers=[

            (r'/',IndexHandler),

        ]

    )

    http_server = tornado.httpserver.HTTPServer(app)

 http_server.listen(options.port)

    tornado.ioloop.IOLoop.current().start()

命令行交互 使用命令行来控制启动

Tornado 支持通过命令行参数来控制 Tornado 的启动形式

代码

define('port', default=8080, help='run port', type=int)

define('version', default='0.0.1', help='version 0.0.1', type=str)

#通过这个来设置动态指定端口.

在命令行中输入:python tor.py --port=8888

使用

python test.py --port=8000

python test.py --version=1.0

python test.py --help

输入和输出

输入:

class TestIndexHandler(tornado.web.RequestHandler):

    def get(self):

        name = self.get_argument('name', 'no') #指定key,value

        self.write('hello '+name)

        print(name)

        name = self.get_arguments('name')

         self.write(' ')

     self.write(','.join(name))

        print(name)

使用:http://127.0.0.1:8000/test?name=budong #key=value&key=value形式.

? 后面便是参数

可以通过: get_argument 和 get_arguments 来获取参数值

输出:

class IndexHandler(tornado.web.RequestHandler):

    def get(self):

        self.write('abc')

从 Tornado 输出到浏览器我们可以使用 write

 

Guess you like

Origin www.cnblogs.com/winfun/p/10972221.html