Home > Backend Development > Python Tutorial > RequestHandler for Python development of Tornado website: output corresponding function

RequestHandler for Python development of Tornado website: output corresponding function

不言
Release: 2018-10-19 17:18:53
forward
2555 people have browsed it
The content of this article is about the RequestHandler of the Tornado website developed in Python: outputting the corresponding function. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The output response function refers to a set of tool functions that generate processing results for the client. Developers call them to control the processing results of the URL. Commonly used output corresponding functions are as follows:

1. RequestHandler.set_status(status_code,reason=None)

Set the return code in HTTP Response. If there is a descriptive statement, it can be assigned to reason parameter.

2. RequestHandler.set_header(name,value)

Set the HTTP header parameters in the HTTP Response in the form of key-value pairs. The Header value configured using set_header will overwrite the previously configured Header.

3. RequestHandler.add_header(name,value)

Set the HTTP header parameters in HTTP Response in the form of key-value pairs. Different from set_header, the Header value configured by add_header will not overwrite the previously configured Header.

4. RequestHandler.write(chunk)

Send the given chunk to the client as HTTP Body. Under normal circumstances, use this function to output a string to the client.
If the given block is a dictionary, the block will be sent to the client in JSON format, and the Content_Type in the HTTP Header will be set to application/json.

5, RequestHandler.finish( chunk=None)

This method notifies Tornado.Response that the generation work has been completed. The chunk parameter is the HTTP body that needs to be passed to the client. After calling finish(), Tornado will send an HTTP Response to the client.
This method is suitable for asynchronous request processing of RequestHandler. In the function of synchronous or coroutine access processing, there is no need to call the finish() function.

6. RequestHandler.render(template_name,**kwargs)

Render the module with the given parameters. You can pass in the template file name and template parameters in this function.
Example

import tornado.web
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        items=["Python","C++","Java"]
        #第一个参数是模板名称,后面是模板参数
        self.render("template.html",title="Tornado Template",items=items)
Copy after login

7. RequestHandler.redirect(url,permanent=False,status=None)

Perform page redirection. During the RequestHandler processing process, the redirect() function can be called at any time to redirect the page.

8. RequestHandler.clear()

Clear all Header and Body content previously written in this request.

9. RequestHandler.set_cookie(name,value)

Set the value of Cookie in Response by key-value pair

10.RequestHandler.clear_all_cookies(path="/", domain=None)

Clear all cookies in this request

The above is the detailed content of RequestHandler for Python development of Tornado website: output corresponding function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template