tornado input and output frame base 02-

01 Output

write

bytes type

class IndexHandler(tornado.web.RequestHandler):

    def get(self):

        self.write(b'Tornado ')

Dictionary Type

user = {

    'Name': 'budong',

    'age': 18

}

  self.write(user)

When passed into the dictionary is not resolved because the json type data will encounter all the contents of all converted into json format string, line breaks do not lead to resolution.

unicode

self.write('Tornado ')

other

= [1, 2, 3, 4]

import json

li = json.dumps (li)

self.write(li)

print (repr (ii))

li = json.loads (li)

print (repr (ii))

Accepts an object

write acceptable bytes, unicode character dictionary and the three objects.

 

If accepted dictionary, the dictionary will be converted into JSON string, and therefore may be acceptable JSON string write

python2 the first line plus # - * - coding: UTF-8 - * - coding format description,

flush

self.flush()

time.sleep(3)

self.write('Tornado ')

Buffer

will first write the content in the buffer, under normal circumstances, when the request is processed automatically output buffer contents to the browser, but may call the flush method, which can directly output the contents of the buffer to the browser without waiting for the completion of the request processing:

render:

HTML return

class TemHandler(tornado.web.RequestHandler):

    def get(self):

        self.render('01in_out.html')

通过  render  可以返回一个 html 文件

Application中的设置:

template_path='templates',

想要 Tornado 能够正确的找到 html 文件,需要在 Application 中指定文件的位置

redirect

跳转路由

class RecHandler(tornado.web.RequestHandler):

    def get(self):

        import time

        time.sleep(3)

        self.redirect(r'/tem')

通过  redirect 可以跳转到指定的路由

路由

(r'/rec', RecHandler),

finish

结束请求

self.finish()

self.write('finish')

finishi后面的代码会执行,但不会显示到浏览器.

当调用 finish 之后,请求处理完成,类似于函数中的 return (注意:请求当中不能出现return) ,其后不能再执行 write ,否则会报错.

 

02 获取请求信息

self.request

handler

class ReqHandler(tornado.web.RequestHandler):

    def get(self):

        self.write(self.request.remote_ip)

        print(self.request.remote_ip)

        print(self.request.full_url())

        print(self.request.request_time())

继承类 tornado.web.RequestHandler 之后 ,可以直接调用 self.request 来获取客户端请求信息

method HTTP请求方法,例如 GET 或 POST
remote_ip 客户端的IP地址,返回值类型为字符串
full_url() 重新构建此请求的完整URL
request_time() 返回此请求执行所花费的时间
uri 请求的完整uri
path 路径部分的uri
query 查询部分的uri
version 请求中指定的HTTP版本,例如“HTTP / 1.1”

application设置debug=True,自动重启修改.

03 输入

添加标题

class GetHandler(tornado.web.RequestHandler):

    def get(self):

        name = self.get_argument('name', 'no')

        self.write(name)

        self.write(' ')       

name = self.get_arguments('name')

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

   #get_arguments把传的所有参数全都传到列表里,get_argument只存最后一个.get_argument底层调用get_arguments,取最后一个值

获取表单数据.

    def post(self, *args, **kwargs):

        name = self.get_argument('name', 'no')

        passwd = self.get_argument('password', 'none')

        self.write('user: %s password: %s' % (name, passwd))

获取 URL 数据

get_argument可以获取 URL (查询字符串)中的参数

获取 body 数据

get_argument可以获取 body (请求体)中的数据,get_argument返回的值始终是unicode

get_query_argument #查询字符串中的参数

self.get_query_argument('name', 'query')

获取查询字符串中参数,对应的也有:get_query_arguments

get_body_argument #获取表单的body

self.get_body_argument('name', 'body')

获取 body 中的参数,对应的也有: get_body_arguments

04 url传参

查询字符串风格

class GetHandler(tornado.web.RequestHandler):

    def get(self):

        name = self.get_argument('name', 'no')

路由 (r'/get', GetHandler) URL http://127.0.0.1:8000/get?name=budong

REST风格

class UserHandler(tornado.web.RequestHandler):

    def get(self, name, age):

        self.write('name: %s age: %s' % (name, age))

class StudentHandler(tornado.web.RequestHandler):

    def get(self, name, number):

        self.write('name: %s number: %s' % (name, number))

路由

(r'/user/(.+)/([0-9]+)', UserHandler), #参数必须按照顺序

(r'/stu/(?P<number>[0-9]+)/(?P<name>.+)', StudentHandler), #?P加了标签

URL

http://127.0.0.1:8000/user/budong/18

http://127.0.0.1:8000/stu/20170001/budong

查询字符串

查询字符串通过在路由后面添加 ? 在加上参数名和参数值来传入参数

REST

通过 / 来分割每个参数,关键在于 get 方法定义

 

驼峰命名法

大驼峰:MainHandler 类

小驼峰:mainHandler

下划线:main_handler python规范

正式代码与导包空1行

类代码与正式代码之间空2行

最外面的函数和代码之间空2行

Guess you like

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