一 :RequestHandler
一般我们继承tornado.web.RequestHandler
1,RequestHandler.initialize()一般用于初始化,第三个字典参数传入
class ProfileHandler(RequestHandler):def initialize(self, database):self.database = databasedef get(self, username):...app = Application([(r'/user/(.*)', ProfileHandler, dict(database=database)),])
2,RequestHandler.prepare()
调用 prepare()。无论使用了哪种 HTTP 方法, prepare 都会被调用到, 因此 这个方法通常会被定义在一个基类中, 然后在子类中重用。prepare可以产生输出 信息。如果它调用了finish(或send_error` 等函数), 那么整个处理流程 就此结束。
3,RequestHandler.on_finish()
重写结束的方法
4,RequestHandler.get
RequestHandler.head
RequestHandler.post
RequestHandler.delete
RequestHandler.path
RequestHandler.put
RequestHandler.options
5,相当于指定,同时也可以增加需要写的函数
class WebDAVHandler(RequestHandler):SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('PROPFIND',)def propfind(self):pass
二:RequestHandler 取出请求结果的方式
1,RequestHandler.get_argument
2,RequestHandler.get_arguments
3,RequeatHandler.get_query_argument
4,RequestHandler.get_query_arguments
5,RequestHandler.
get_body_argument
6,RequestHandler.
get_body_arguments
7,RequestHandler.
decode_argument
8,RequestHandler.
request
9,RequestHandler.
path_args
10,RequestHandler.
path_kwargs
三 RequestHandler 设置值方式
1,RequestHandler.
set_status
(status_code, reason=None)
2,RequestHandler.
set_header
(name, value)
3,RequestHandler.
add_header
(name, value)
4,RequestHandler.
clear_header
(name)
5,RequestHandler.
set_default_headers
()[
6,RequestHandler.
write
(chunk)
7,RequestHandler.
flush
(include_footers=False, callback=None)(缓冲数据等)
8,RequestHandler.
finish
(chunk=None)
9,RequestHandler.
render
(template_name, **kwargs)
10,RequestHandler.
render_string
(template_name, **kwargs)
11,RequestHandler.
get_template_namespace
()
12,RequestHandler.
redirect
(url, permanent=False, status=None)
13,RequestHandler.
send_error
(status_code=500, **kwargs)
14,RequestHandler.
write_error
(status_code, **kwargs
15,RequestHandler.
clear
()
16,RequestHandler.
data_received
(chunk)
四:Cookies
1,RequestHandler.
cookies
2,RequestHandler.
get_cookie
(name, default=None)
3,RequestHandler.
set_cookie
(name, value, domain=None, expires=None, path='/', expires_days=None,**kwargs)
4,RequestHandler.
clear_cookie
(name, path='/', domain=None)
5,RequestHandler.
clear_all_cookies
(path='/', domain=None)
6,RequestHandler.
get_secure_cookie
(name, value=None, max_age_days=31, min_version=None)加密cookies
7,RequestHandler.
get_secure_cookie_key_version
(name, value=None)
8,RequestHandler.
set_secure_cookie
(name, value, expires_days=30, version=None, **kwargs)
9,tornado.web.
MIN_SUPPORTED_SIGNED_VALUE_VERSION
= 1
10,tornado.web.
MAX_SUPPORTED_SIGNED_VALUE_VERSION
= 2
11,tornado.web.
DEFAULT_SIGNED_VALUE_VERSION
= 2
12,tornado.web.
DEFAULT_SIGNED_VALUE_MIN_VERSION
= 1
五:其他
当前通过认证的用户在请求处理器的 self.current_user
当中, 而且还存在于模版中的current_user
. 默认情况下, current_user
的值为 None
.
为了在你的应用程序中实现用户认证, 你需要覆盖请求控制器中的 get_current_user()
方法 来确认怎样获取当前登陆的用户, 例如, 从 cookie 的值中获取该信息. 下面这个例子展示了通过用户的昵称来确定用户身份, 值被保存在 cookies 中:
class BaseHandler(tornado.web.RequestHandler):def get_current_user(self):return self.get_secure_cookie("user")class MainHandler(BaseHandler):def get(self):if not self.current_user:self.redirect("/login")returnname = tornado.escape.xhtml_escape(self.current_user)self.write("Hello, " + name)class LoginHandler(BaseHandler):def get(self):self.write('<html><body><form action="/login" method="post">''Name: <input type="text" name="name">''<input type="submit" value="Sign in">''</form></body></html>')def post(self):self.set_secure_cookie("user", self.get_argument("name"))self.redirect("/")application = tornado.web.Application([(r"/", MainHandler),(r"/login", LoginHandler),
], cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__")
1,RequestHandler.current_user()用于检查用户登录
def get_current_user(self):user_cookie = self.get_secure_cookie("user")if user_cookie:return json.loads(user_cookie)return None
如果用prepare()初始化必须是gen.coroutine,,,结束yeile才可以以
@gen.coroutine def prepare(self):user_id_cookie = self.get_secure_cookie("user_id")if user_id_cookie:self.current_user = yield load_user(user_id_cookie)
六:装饰器
1,tornado.web.
asynchronous
(method)
一般适用于http开头的请求方式
class MyRequestHandler(RequestHandler):@asynchronousdef get(self):http = httpclient.AsyncHTTPClient()http.fetch("http://friendfeed.com/", self._on_download)def _on_download(self, response):self.write("Downloaded!")self.finish()
2,tornado.web.
authenticated
(method)
用于判断是否登录状态
3,classtornado.web.
FallbackHandler
(application, request, **kwargs)
wsgi_app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler()) application = tornado.web.Application([(r"/foo", FooHandler),(r".*", FallbackHandler, dict(fallback=wsgi_app), ])
下面列出RequestHandler中需要重写的方法
RequestHandler.initialize() RequestHandler.prepare() RequestHandler.get() RequestHandler.post() RequestHandler.put() RequestHandler.delete() RequestHandler.option() RequestHandler.patch() RequestHandler.head() RequestHandler.on_finish() RequestHandler.set_default_headers() RequestHandler.get_template_namespace() RequestHandler.create_template_loader() RequestHandler.write_error(status_code, **kwargs) RequestHandler.get_user_locale() RequestHandler.get_current_user() RequestHandler.get_login_url() RequestHandler.get_template_path() RequestHandler.compute_etag() RequestHandler.data_received() RequestHandler.log_exception()
转载自 https://www.cnblogs.com/1204guo/p/8549459.html