输入和输出

输入输出:

class MainHandler(RequestHandler):
    def get(self):
        self.write('hello world')
        self.write(123)   #这一行会报错

报错信息如下:

TypeError: write() only accepts bytes, unicode, and dict objects

输入输出:
  输出:
    write能写的几种格式    bytes, unicode, and dict objects
    bytes
    字符串
    字典
    json
    flush
    渲染模板render  添加模板位置templates
    路由跳转redirect
    结束:finish

class MainHandler(RequestHandler):
    def get(self):
        self.write('hello world')
        self.write('<br/>')
        self.write(b'hello world')  #bytes
        self.write('<br/>')
        # self.write(123)  #报错
        self.flush()  #强制吧缓冲区的内容刷新到浏览器
        di = {
            'name': 'shiwei',
            'age': 23
        }
        self.write(di)  # dictory #当write方法向浏览器输出字典的时候,其他所有的格式都不被解析,
          #当浏览器输出字典时候,其他的全部被当成json字符串的一部分输出

        li = ['taka',52]
        # self.write(li) #列表也不能写入
        self.write(str(li))  #将列表转换成字符串

        li = json.dumps(li)  #转换成json的字符串
        self.write(li)
        self.finish()
        self.write('1111')
        #调用finish之后,不会输出到浏览器上,代码依然会被执行性
        # 报错raise RuntimeError("Cannot write() after finish()")

        #字节格式,字符串,字典
        #b'hello'  字节
        #'hello'
        #{'name':'shiwei'}


class TemHandler(RequestHandler):
    def get(self):
        self.render('in_out.html') #将页面写到一个文件里面,当我们访问这个路由的时候读取文件


class RedHandler(RequestHandler):
    def get(self):
        time.sleep(5)
        self.redirect('/tem')


application = tornado.web.Application(
    handlers=[
        (r'/', MainHandler),  
        (r'/tem', TemHandler),  
        (r'/red', RedHandler),
    ],
    template_path = 'templates', #指定静态模板的文件夹
    debug=True  #方便调试
) 

获取请求信息

class ReqHandler(RequestHandler):
    def get(self):
        print(self.request)
        self.write(self.request.remote_ip)


application = tornado.web.Application(
    handlers=[
        (r'/req', ReqHandler),
    ],
    template_path = 'templates', #指定静态模板的文件夹
    debug=True  #方便调试
) 

 self.request中包含所有的请求信息:

可以使用的方法:

输入:

get_argument只能获取输入的最后一个参数,调用了get_arguments,并且取列表的最后一个元素,获取 URL 和 Body中数据

get_arguments 获取输入的列表,复选框的时候使用

-- 获取URL数据

get_argument  

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

-- 获取body数据

get_argument

可以获取body(请求体)中的数据

get_argument返回的值始终是unicode

02-input_output.py

class GetHandler(RequestHandler):
    def get(self):
        self.render('in_out.html')

    def post(self):
        # name = self.get_argument('name','')
        name = self.get_body_argument('name','')
        self.write(name)
        self.write('<br/>')
        password = self.get_argument('password','')
        self.write(password)


application = tornado.web.Application(
    handlers=[
        (r'/get', GetHandler),

    ],

in_out.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tornado</title>
</head>
<body>
<form method="post" action="/get">
    <p>用户名<input type="text" name="name"></p>
    <p>密码<input type="password" name="password"></p>
    <input type="submit">
</form>
</body>
</html>

 URL传参

1.REST风格查询

class SubHandler(RequestHandler):
    def get(self,name,age):
        self.write('name:%s <br/> age:%s'%(name,age))


application = tornado.web.Application(
    handlers=[
        (r'/sub/(?P<name>.+)/(?P<age>[0-9]+)', SubHandler),
    ],

请求结果如下:

2.字符串风格,?key=value

class SubHandler(RequestHandler):
    def get(self):
        name = self.get_argument('name','')
     
self.write(name)

application = tornado.web.Application(
    handlers=[
        (r'/sub/', SubHandler),
    ],

请求结果如下:

原文地址:https://www.cnblogs.com/taoge188/p/10625554.html