tornado的输入

  • 查询字符串(query string),形如key1=value1&key2=value2;
  • 请求体(body)中发送的数据,比如表单数据、json、xml;
  • 提取uri的特定部分,如/blogs/2016/09/0001,可以在服务器端的路由中用正则表达式截取;
  • 在http报文的头(header)中增加自定义字段,如X-XSRFToken=itcast。

  1.获取查询字符串参数

    get_query_argument(键名,default=默认值   ,strip=是否过滤两边空白字符,默认True)

    get_query_arguments()同上。当一个键对应多个值时返回列表,上方法仅返回最后一个

  2.获取请求体参数

    get_body_argument()      同上

    get_body_arguments()    同上

  说明

  对于请求体中的数据要求为字符串,且格式为表单编码格式(与url中的请求字符串格式相同),即    key1=value1&key2=value2,HTTP报文头Header中的"Content-Type"为application/x-www-form-urlencoded 或 multipart/form-data。对于请求体数据为json或xml的,无法通过这两个方法获取。

  3.以上两类方法的整合

    get_argument()           同上

    get_arguments()         同上

  以上方法返回的都是unicode字符串

  4.关于请求的其他信息

RequestHandler.request 对象存储了关于请求的相关信息,具体属性有:

    • method HTTP的请求方式,如GET或POST;
    • host 被请求的主机名;
    • uri 请求的完整资源标示,包括路径和查询字符串;
    • path 请求的路径部分;
    • query 请求的查询字符串部分;
    • version 使用的HTTP版本;
    • headers 请求的协议头,是类字典型的对象,支持关键字索引的方式获取特定协议头信息,例如:request.headers["Content-Type"]
    • body 请求体数据;
    • remote_ip 客户端的IP地址;
    • files 用户上传的文件,为字典类型,型如:
      {
        "form_filename1":[<tornado.httputil.HTTPFile>, <tornado.httputil.HTTPFile>],
        "form_filename2":[<tornado.httputil.HTTPFile>,],
        ... 
      }
      
      tornado.httputil.HTTPFile是接收到的文件对象,它有三个属性:
      • filename 文件的实际名字,与form_filename1不同,字典中的键名代表的是表单对应项的名字;
      • body 文件的数据实体;
      • content_type 文件的类型。 这三个对象属性可以像字典一样支持关键字索引,如request.files["form_filename1"][0]["body"]。

import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import RequestHandler

define("port", default=8000, type=int, help="run server on the given port.")

class IndexHandler(RequestHandler):
    def get(self):
        self.write("hello itcast.")

class UploadHandler(RequestHandler): 
    def post(self):
        files = self.request.files
        img_files = files.get('img')
        if img_files:
            img_file = img_files[0]["body"]
            file = open("./itcast", 'w+')
            file.write(img_file)
            file.close()
        self.write("OK")

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (r"/", IndexHandler),
        (r"/upload", UploadHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

 

  5.tornado支持正则提取url中的参数

app = tornado.web.Application([
        (r"/", IndexHandler),
        (r"/sub-city/(.+)/([a-z]+)", SubjectCityHandler), # 无名方式
        (r"/sub-date/(?P<subject>.+)/(?P<date>\d+)", SubjectDateHandler), # 命名方式
    ])

    与django类似