tornado 之 RequestHandler(请求)

RequestHandler

from tornado.web import ReuqestHandler

一、利用HTTP协议想服务器传递参数

  • 提取url的特定部分
  • get方式传递参数
    • http://127.0.0.1:8000/qzk/?name=qzk&age=18
    • self.get_query_argument(name,default=ARG_DEFAULT,strip=True)
    • 参数:
      • name:从get请求参数字符串中返回指定参数的值,如果穿线过同名参数,则返回最后一个
      • default:设置为未传的name参数是返回默认的值,如果default也没有设置,会抛出异常 tornado.web.MissingArgumentError
      • strip:表示是否过滤掉左右两边的空白字符,默认为True
    • self.get_query_arguments(name,strip=True)
  • post方式传递参数
    • http://127.0.0.1:8000/userinfo
    • self.get_body_argument(name,default=ARG_DEFAULT,strip=True)
    • 参数:
      • name:从get请求参数字符串中返回指定参数的值,如果穿线过同名参数,则返回最后一个
      • default:设置为未传的name参数是返回默认的值,如果default也没有设置,会抛出异常 tornado.web.MissingArgumentError
      • strip:表示是否过滤掉左右两边的空白字符,默认为True
    • self.get_body_arguments(name,strip=True)
  • 既可以获取get请求,也可以获取post请求
    • self.get_argument(name,default=ARG_DEFAULT,strip=True)
    • self.get_arguments((name,strip=True)
    • 注意:一般我们不太会用该方法,因为不太容易区分get/post
  • 在http报文的头中增加自定义的字段

二、request对象

  • 作用:存储关于请求的相关信息
  • 属性:
    • method:HTTP请求的方式
    • host:被请求的主机名
    • uri:请求的完整资源地址,包括路径和get查询参数的部分
    • path:请求的路径部分
    • query:请求的参数部分
    • version:使用的http版本
    • headers:请求的协议头,是一个字典类型
    • body:请求体数据
    • remote_ip:客户端的ip
    • files:用户上传的文件,字典类型

三、tornado.httputil.HTTPFile对象

  • 作用:接收到的文件对象
  • 属性:
    • filename:文件的实际名字
    • body:文件的数据实体
    • content_type:文件的类型
  • 示例:
"""url-application"""
# -*- coding: utf-8 -*-

import tornado.web

from views import index
import config


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/uploadfiles', index.UploadFilesHandler),
        ]
        super(Application, self).__init__(handlers, **config.settings)
import os
import config


class UploadFilesHandler(RequestHandler):
    def get(self, *args, **kwargs):
        self.render('upload.html')
    
    def post(self, *args, **kwargs):
        fileDict = self.request.files
        print(fileDict)
        for inputname in fileDict:
            filelist = fileDict[inputname]
            for file_obj in filelist:
                filePath = os.path.join(config.BASE_DIR, 'media/' + file_obj.filename)
                with open(filePath, 'wb') as f:
                    f.write(file_obj.body)
        self.write('ok')
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>upload</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<form action="/uploadfiles" method="post" enctype="multipart/form-data">
    <br>
    <input type="file" name="file">
    <br>
    <input type="file" name="file">
    <br>
    <input type="file" name="img">
    <br>
    <!--<input type="image" name="image">-->
    <button class="btn btn-danger" >提交</button>
</form>
</body>
</html>

打印结果:

{
'file': [
        {
            'filename': 'a.txt', 
            'body': b'qwe', 
            'content_type': 'text/plain'
        }, 
        {
            'filename': 'b.txt', 
            'body': b'asd', 
            'content_type': 'text/plain'
        }
    ],
'img':[
    {
        'filename':'b2.png',
        'body':b'...',
        'content_type':'image/png'
    }
]
}

转载于:https://www.cnblogs.com/qianzhengkai/p/11343394.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值