def redirect(self, url, permanent=False, status=None)
重定向到给定的URL(可以选择相对路径).
如果指定了 ``status`` 参数, 这个值将作为HTTP状态码; 否则
将通过 ``permanent`` 参数选择301 (永久) 或者 302 (临时).
默认是 302 (临时重定向).
if self._headers_written:
raise Exception("Cannot redirect after headers have been written")
if status is None:
status = 301 if permanent else 302
else:
assert isinstance(status, int) and 300 <= status <= 399
self.set_status(status)
self.set_header("Location", utf8(url))
self.finish()
def write(self, chunk):
"""把给定块写到输出buffer.
为了把输出写到网络, 使用下面的flush()方法.
如果给定的块是一个字典, 我们会把它作为JSON来写同时会把响应头
设置为 ``application/json``. (如果你写JSON但是设置不同的
``Content-Type``, 可以调用set_header *在调用write()之后* ).
注意列表不能转换为JSON 因为一个潜在的跨域安全漏洞. 所有的JSON
输出应该包在一个字典中. 更多细节参考
http://haacked.com/archive/2009/06/25/json-hijacking.aspx/ 和
https://github.com/facebook/tornado/issues/1009
"""
if self._finished:
raise RuntimeError("Cannot write() after finish()")
if not isinstance(chunk, (bytes, unicode_type, dict)):
message = "write() only accepts bytes, unicode, and dict objects"
if isinstance(chunk, list):
message += ". Lists not accepted for security reasons; see http://www.tornadoweb.org/en/stable/web.html#tornado.web.RequestHandler.write"
raise TypeError(message)
if isinstance(chunk, dict):
chunk = escape.json_encode(chunk)
self.set_header("Content-Type", "application/json; charset=UTF-8")
chunk = utf8(chunk)
self._write_buffer.append(chunk)
网友评论