tornado基础:深入tornado | tornado |《python学习之路》| python 技术论坛-金年会app官方网

未匹配的标注

application

settings

前面的学习中,我们在创建tornado.web.application的对象时,传入了第一个参数——路由映射列表。实际上application类的构造函数还接收很多关于tornado web应用的配置参数,在后面的学习中我们用到的地方会为大家介绍。

我们先来学习一个参数:

debug,设置tornado是否工作在调试模式,默认为false即工作在生产模式。当设置debug=true 后,tornado会工作在调试/开发模式,在此种模式下,tornado为方便我们开发而提供了几种特性:

  • 自动重启,tornado应用会监控我们的源代码文件,当有改动保存后便会重启程序,这可以减少我们手动重启程序的次数。需要注意的是,一旦我们保存的更改有错误,自动重启会导致程序报错而退出,从而需要我们保存修正错误后手动启动程序。这一特性也可单独通过autoreload=true设置;
  • 取消缓存编译的模板,可以单独通过compiled_template_cache=false来设置;
  • 取消缓存静态文件hash值,可以单独通过static_hash_cache=false来设置;
  • 提供追踪信息,当requesthandler或者其子类抛出一个异常而未被捕获后,会生成一个包含追踪信息的页面,可以单独通过serve_traceback=true来设置。

使用debug参数的方法:

import tornado.web
app = tornado.web.application([], debug=true)

路由映射

先前我们在构建路由映射列表的时候,使用的是二元元组,如:

[(r"/", indexhandler),]

对于这个映射列表中的路由,实际上还可以传入多个信息,如:

[
    (r"/", indexhandler),
    (r"/cpp", itcasthandler, {"subject":"c  "}),
    url(r"/python", itcasthandler, {"subject":"python"}, name="python_url")
]

对于路由中的字典,会传入到对应的requesthandler的initialize()方法中:

from tornado.web import requesthandler
class itcasthandler(requesthandler):
    def initialize(self, subject):
        self.subject = subject
    def get(self):
        self.write(self.subject)

对于路由中的name字段,注意此时不能再使用元组,而应使用tornado.web.url来构建。name是给该路由起一个名字,可以通过调用requesthandler.reverse_来获取该名子对应的url。

# coding:utf-8
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import url, requesthandler
define("port", default=8000, type=int, help="run server on the given port.")
class indexhandler(requesthandler):
    def get(self):
        python_url = self.reverse_url("python_url")
        self.write('' %
                   python_url)
class itcasthandler(requesthandler):
    def initialize(self, subject):
        self.subject = subject
    def get(self):
        self.write(self.subject)
if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.application([
            (r"/", indexhandler),
            (r"/cpp", itcasthandler, {"subject":"c  "}),
            url(r"/python", itcasthandler, {"subject":"python"}, name="python_url")
        ],
        debug = true)
    http_server = tornado.httpserver.httpserver(app)
    http_server.listen(options.port)
    tornado.ioloop.ioloop.current().start()

输入

简介

下面几节主要讲解tornado.web.requesthandler。

回想一下,利用http协议向服务器传参有几种途径?

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

我们现在来看下tornado中为我们提供了哪些方法来获取请求的信息。

获取查询字符串参数

get_query_argument(name, default=_arg_default, strip=true)

从请求的查询字符串中返回指定参数name的值,如果出现多个同名参数,则返回最后一个的值。

default为设值未传name参数时返回的默认值,如若default也未设置,则会抛出tornado.web.missingargumenterror异常。

strip表示是否过滤掉左右两边的空白字符,默认为过滤。

get_query_arguments(name, strip=true)

从请求的查询字符串中返回指定参数name的值,注意返回的是list列表(即使对应name参数只有一个值)。若未找到name参数,则返回空列表[]。

strip同前,不再赘述。

获取请求体参数

get_body_argument(name, default=_arg_default, strip=true)

从请求体中返回指定参数name的值,如果出现多个同名参数,则返回最后一个的值。

default与strip同前,不再赘述。

get_body_arguments(name, strip=true)

从请求体中返回指定参数name的值,注意返回的是list列表(即使对应name参数只有一个值)。若未找到name参数,则返回空列表[]。

strip同前,不再赘述。

说明

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

前两类方法的整合

get_argument(name, default=_arg_default, strip=true)

从请求体和查询字符串中返回指定参数name的值,如果出现多个同名参数,则返回最后一个的值。

default与strip同前,不再赘述。

get_arguments(name, strip=true)

从请求体和查询字符串中返回指定参数name的值,注意返回的是list列表(即使对应name参数只有一个值)。若未找到name参数,则返回空列表[]。

strip同前,不再赘述。

说明

对于请求体中数据的要求同前。 这两个方法最常用

用代码来看上述六中方法的使用:

# coding:utf-8
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import requesthandler, missingargumenterror
define("port", default=8000, type=int, help="run server on the given port.")
class indexhandler(requesthandler):
    def post(self):
        query_arg = self.get_query_argument("a")
        query_args = self.get_query_arguments("a")
        body_arg = self.get_body_argument("a")
        body_args = self.get_body_arguments("a", strip=false)
        arg = self.get_argument("a")
        args = self.get_arguments("a")
        default_arg = self.get_argument("b", "itcast")
        default_args = self.get_arguments("b")
        try:
            missing_arg = self.get_argument("c")
        except missingargumenterror as e:
            missing_arg = "we catched the missingargumenterror!"
            print e
        missing_args = self.get_arguments("c")
        rep = "query_arg:%s
"
% query_arg rep = "query_args:%s
"
% query_args rep = "body_arg:%s
"
% body_arg rep = "body_args:%s
"
% body_args rep = "arg:%s
"
% arg rep = "args:%s
"
% args rep = "default_arg:%s
"
% default_arg rep = "default_args:%s
"
% default_args rep = "missing_arg:%s
"
% missing_arg rep = "missing_args:%s
"
% missing_args self.write(rep) if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.application([ (r"/", indexhandler), ]) http_server = tornado.httpserver.httpserver(app) http_server.listen(options.port) tornado.ioloop.ioloop.current().start()

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

思考

  1. 什么时候设置default,什么时候不设置default?
  2. default的默认值_arg_default是什么?
  3. 什么时候使用strip,亦即什么时候要截断空白字符,什么时候不需要?

关于请求的其他信息

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”]。

我们来实现一个上传文件并保存在服务器本地的小程序upload.py:

# coding:utf-8
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()

正则提取uri

tornado中对于路由映射也支持正则提取uri,提取出来的参数会作为requesthandler中对应请求方式的成员方法参数。若在正则表达式中定义了名字,则参数按名传递;若未定义名字,则参数按顺序传递。提取出来的参数会作为对应请求方式的成员方法的参数。

# coding:utf-8
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 subjectcityhandler(requesthandler):
    def get(self, subject, city):
        self.write(("subject: %s
city: %s"
% (subject, city))) class subjectdatehandler(requesthandler): def get(self, date, subject): self.write(("date: %s
subject: %s"
% (date, subject))) if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.application([ (r"/", indexhandler), (r"/sub-city/(. )/([a-z] )", subjectcityhandler), # 无名方式 (r"/sub-date/(?p. )/(?p\d )", subjectdatehandler), # 命名方式 ]) http_server = tornado.httpserver.httpserver(app) http_server.listen(options.port) tornado.ioloop.ioloop.current().start()

建议:提取多个值时最好用命名方式。

输出

write(chunk)

将chunk数据写到输出缓冲区。如我们在之前的示例代码中写的:

class indexhandler(requesthandler):
    def get(self):
        self.write("hello itcast!")

想一想,可不可以在同一个处理方法中多次使用write方法?下面的代码会出现什么效果?

class indexhandler(requesthandler):
    def get(self):
        self.write("hello itcast 1!")
        self.write("hello itcast 2!")
        self.write("hello itcast 3!")

write方法是写到缓冲区的,我们可以像写文件一样多次使用write方法不断追加响应内容,最终所有写到缓冲区的内容一起作为本次请求的响应输出。

想一想,如何利用write方法写json数据?

import json
class indexhandler(requesthandler):
    def get(self):
        stu = {
            "name":"zhangsan",
            "age":24,
            "gender":1,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)

实际上,我们可以不用自己手动去做json序列化,当write方法检测到我们传入的chunk参数是字典类型后,会自动帮我们转换为json字符串。

class indexhandler(requesthandler):
    def get(self):
        stu = {
            "name":"zhangsan",
            "age":24,
            "gender":1,
        }
        self.write(stu)

两种方式有什么差异?

对比一下两种方式的响应头header中content-type字段,自己手动序列化时为content-type:text/html; charset=utf-8,而采用write方法时为content-type:application/json; charset=utf-8

write方法除了帮我们将字典转换为json字符串之外,还帮我们将content-type设置为application/json; charset=utf-8

set_header(name, value)

利用set_header(name, value)方法,可以手动设置一个名为name、值为value的响应头header字段。

用set_header方法来完成上面write所做的工作。

import json
class indexhandler(requesthandler):
    def get(self):
        stu = {
            "name":"zhangsan",
            "age":24,
            "gender":1,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)
        self.set_header("content-type", "application/json; charset=utf-8")

set_default_headers()

该方法会在进入http处理方法前先被调用,可以重写此方法来预先设置默认的headers。注意:在http处理方法中使用set_header()方法会覆盖掉在set_default_headers()方法中设置的同名header。

class indexhandler(requesthandler):
    def set_default_headers(self):
        print "执行了set_default_headers()"
        # 设置get与post方式的默认响应体格式为json
        self.set_header("content-type", "application/json; charset=utf-8")
        # 设置一个名为itcast、值为python的header
        self.set_header("itcast", "python")
    def get(self):
        print "执行了get()"
        stu = {
            "name":"zhangsan",
            "age":24,
            "gender":1,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)
        self.set_header("itcast", "i love python") # 注意此处重写了header中的itcast字段
    def post(self):
        print "执行了post()"
        stu = {
            "name":"zhangsan",
            "age":24,
            "gender":1,
        }
        stu_json = json.dumps(stu)
        self.write(stu_json)

set_status(status_code, reason=none)

为响应设置状态码。

参数说明:

  • status_code int类型,状态码,若reason为none,则状态码必须为下表中的。
  • reason string类型,描述状态码的词组,若为none,则会被自动填充为下表中的内容。
code enum name details
100 continue http/1.1 rfc 7231, section 6.2.1
101 switching_protocols http/1.1 rfc 7231, section 6.2.2
102 processing webdav rfc 2518, section 10.1
200 ok http/1.1 rfc 7231, section 6.3.1
201 created http/1.1 rfc 7231, section 6.3.2
202 accepted http/1.1 rfc 7231, section 6.3.3
203 non_authoritative_information http/1.1 rfc 7231, section 6.3.4
204 no_content http/1.1 rfc 7231, section 6.3.5
205 reset_content http/1.1 rfc 7231, section 6.3.6
206 partial_content http/1.1 rfc 7233, section 4.1
207 multi_status webdav rfc 4918, section 11.1
208 already_reported webdav binding extensions rfc 5842, section 7.1 (experimental)
226 im_used delta encoding in http rfc 3229, section 10.4.1
300 multiple_choices http/1.1 rfc 7231, section 6.4.1
301 moved_permanently http/1.1 rfc 7231, section 6.4.2
302 found http/1.1 rfc 7231, section 6.4.3
303 see_other http/1.1 rfc 7231, section 6.4.4
304 not_modified http/1.1 rfc 7232, section 4.1
305 use_proxy http/1.1 rfc 7231, section 6.4.5
307 temporary_redirect http/1.1 rfc 7231, section 6.4.7
308 permanent_redirect permanent redirect rfc 7238, section 3 (experimental)
400 bad_request http/1.1 rfc 7231, section 6.5.1
401 unauthorized http/1.1 authentication rfc 7235, section 3.1
402 payment_required http/1.1 rfc 7231, section 6.5.2
403 forbidden http/1.1 rfc 7231, section 6.5.3
404 not_found http/1.1 rfc 7231, section 6.5.4
405 method_not_allowed http/1.1 rfc 7231, section 6.5.5
406 not_acceptable http/1.1 rfc 7231, section 6.5.6
407 proxy_authentication_required http/1.1 authentication rfc 7235, section 3.2
408 request_timeout http/1.1 rfc 7231, section 6.5.7
409 conflict http/1.1 rfc 7231, section 6.5.8
410 gone http/1.1 rfc 7231, section 6.5.9
411 length_required http/1.1 rfc 7231, section 6.5.10
412 precondition_failed http/1.1 rfc 7232, section 4.2
413 request_entity_too_large http/1.1 rfc 7231, section 6.5.11
414 request_uri_too_long http/1.1 rfc 7231, section 6.5.12
415 unsupported_media_type http/1.1 rfc 7231, section 6.5.13
416 request_range_not_satisfiable http/1.1 range requests rfc 7233, section 4.4
417 expectation_failed http/1.1 rfc 7231, section 6.5.14
422 unprocessable_entity webdav rfc 4918, section 11.2
423 locked webdav rfc 4918, section 11.3
424 failed_dependency webdav rfc 4918, section 11.4
426 upgrade_required http/1.1 rfc 7231, section 6.5.15
428 precondition_required additional http status codes rfc 6585
429 too_many_requests additional http status codes rfc 6585
431 request_header_fields_too_large additional http status codes rfc 6585
500 internal_server_error http/1.1 rfc 7231, section 6.6.1
501 not_implemented http/1.1 rfc 7231, section 6.6.2
502 bad_gateway http/1.1 rfc 7231, section 6.6.3
503 service_unavailable http/1.1 rfc 7231, section 6.6.4
504 gateway_timeout http/1.1 rfc 7231, section 6.6.5
505 http_version_not_supported http/1.1 rfc 7231, section 6.6.6
506 variant_also_negotiates transparent content negotiation in http rfc 2295, section 8.1 (experimental)
507 insufficient_storage webdav rfc 4918, section 11.5
508 loop_detected webdav binding extensions rfc 5842, section 7.2 (experimental)
510 not_extended an http extension framework rfc 2774, section 7 (experimental)
511 network_authentication_required additional http status codes rfc 6585, section 6
class err404handler(requesthandler):
    """对应/err/404"""
    def get(self):
        self.write("hello itcast")
        self.set_status(404) # 标准状态码,不用设置reason
class err210handler(requesthandler):
    """对应/err/210"""
    def get(self):
        self.write("hello itcast")
        self.set_status(210, "itcast error") # 非标准状态码,设置了reason
class err211handler(requesthandler):
    """对应/err/211"""
    def get(self):
        self.write("hello itcast")
        self.set_status(211) # 非标准状态码,未设置reason,错误

redirect(url)

告知浏览器跳转到url。

class indexhandler(requesthandler):
    """对应/"""
    def get(self):
        self.write("金年会app官方网主页")
class loginhandler(requesthandler):
    """对应/login"""
    def get(self):
        self.write('
'
) def post(self): self.redirect("/")

send_error(status_code=500, **kwargs)

抛出http错误状态码status_code,默认为500,kwargs为可变命名参数。使用send_error抛出错误后tornado会调用write_error()方法进行处理,并返回给浏览器处理后的错误页面。

class indexhandler(requesthandler):
    def get(self):
        self.write("金年会app官方网主页")
        self.send_error(404, content="出现404错误")

默认的write\_error()方法不会处理send\_error抛出的kwargs参数,即上面的代码中content="出现404错误"是没有意义的。

尝试下面的代码会出现什么问题?

class indexhandler(requesthandler):
    def get(self):
        self.write("金年会app官方网主页")
        self.send_error(404, content="出现404错误")
        self.write("结束") # 我们在send_error再次向输出缓冲区写内容

使用send_error()方法后就不要再向输出缓冲区写内容了!

write_error(status_code, **kwargs)

用来处理send_error抛出的错误信息并返回给浏览器错误信息页面。可以重写此方法来定制自己的错误显示页面。

class indexhandler(requesthandler):
    def get(self):
        err_code = self.get_argument("code", none) # 注意返回的是unicode字符串,下同
        err_title = self.get_argument("title", "")
        err_content = self.get_argument("content", "")
        if err_code:
            self.send_error(err_code, title=err_title, content=err_content)
        else:
            self.write("金年会app官方网主页")
    def write_error(self, status_code, **kwargs):
        self.write(u"")
        self.write(u"

错误名:%s

"
% kwargs["title"]) self.write(u"

错误详情:%s

"
% kwargs["content"])

接口与调用顺序

下面的接口方法是由tornado框架进行调用的,我们可以选择性的重写这些方法。

initialize()

对应每个请求的处理类handler在构造一个实例后首先执行initialize()方法。在讲输入时提到,路由映射中的第三个字典型参数会作为该方法的命名参数传递,如:

class profilehandler(requesthandler):
    def initialize(self, database):
        self.database = database
    def get(self):
        ...
app = application([
    (r'/user/(.*)', profilehandler, dict(database=database)),
    ])

此方法通常用来初始化参数(对象属性),很少使用。

prepare()

预处理,即在执行对应请求方式的http方法(如get、post等)前先执行,注意:不论以何种http方式请求,都会执行prepare()方法

以预处理请求体中的json数据为例:

import json
class indexhandler(requesthandler):
    def prepare(self):
        if self.request.headers.get("content-type").startswith("application/json"):
            self.json_dict = json.loads(self.request.body)
        else:
            self.json_dict = none
    def post(self):
        if self.json_dict:
            for key, value in self.json_dict.items():
                self.write("

%s

%s

"
% (key, value)) def put(self): if self.json_dict: for key, value in self.json_dict.items(): self.write("

%s

%s

"
% (key, value))

http方法

方法 描述
get 请求指定的页面信息,并返回实体主体。
head 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
post 向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。post请求可能会导致新的资源的建立和/或已有资源的修改。
delete 请求服务器删除指定的内容。
patch 请求修改局部数据。
put 从客户端向服务器传送的数据取代指定的文档的内容。
options 返回给定url支持的所有http方法。

on_finish()

在请求处理结束后调用,即在调用http方法后调用。通常该方法用来进行资源清理释放或处理日志等。注意:请尽量不要在此方法中进行响应输出。

调用顺序

我们通过一段程序来看上面这些接口的调用顺序。

class indexhandler(requesthandler):
    def initialize(self):
        print "调用了initialize()"
    def prepare(self):
        print "调用了prepare()"
    def set_default_headers(self):
        print "调用了set_default_headers()"
    def write_error(self, status_code, **kwargs):
        print "调用了write_error()"
    def get(self):
        print "调用了get()"
    def post(self):
        print "调用了post()"
        self.send_error(200)  # 注意此出抛出了错误
    def on_finish(self):
        print "调用了on_finish()"

在正常情况未抛出错误时,调用顺序为:

  1. set_defautl_headers()
  2. initialize()
  3. prepare()
  4. http方法
  5. on_finish()

在有错误抛出时,调用顺序为:

  1. set_default_headers()
  2. initialize()
  3. prepare()
  4. http方法
  5. set_default_headers()
  6. write_error()
  7. on_finish()

本文章首发在 金年会app官方网 网站上。

上一篇 下一篇
讨论数量: 0



暂无话题~
网站地图