Home  >  Article  >  Backend Development  >  RequestHandler for Python development of Tornado website: input capture

RequestHandler for Python development of Tornado website: input capture

不言
不言forward
2018-10-19 17:16:052601browse
The content of this article is about the RequestHandler of Tornado website developed in Python: input capture. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Input capture refers to the tool functions and properties used in RequestHandler to obtain client input. For example, getting URL parameters, Post submission parameters, etc.

1. get_argument(name), get_arguments(name)

RequestHandler.get_argument(name) and RequestHandler.get_arguments(name) both return the value of the given parameter. get_argument obtains a single value, while get_arguments is used when there are multiple values ​​in the parameter and returns a list of multiple values.
Note: Use these two methods to obtain the parameter collection of the parameters queried in the URL and the parameters submitted by POST.

2. get_query_argument(name), get_query_arguments(name)

The functions are similar to the above two methods. The only difference is that these two methods only query parameters from the URL.

3. get_body_argument(name), get_body_arguments(name)

The functions are still similar to the above four methods. The only difference is that these two methods only query from the parameters submitted by POST.

Tip: In actual development, the two methods get_argument and get_arguments are generally used because they will contain the query results of other methods.

4. get_cookie(name,default=None)

Get the cookie value based on the cookie name

5. RequestHandler.request

Return tornado.httputil Attributes of the .HTTPServerRequest object instance. All information about the HTTP request can be obtained through this object, such as:

from tornado.web import Application, RequestHandler
import tornado.ioloop


class DetailHandler(RequestHandler):
    def get(self):
        ip = self.request.remote_ip  # 获取客户端的IP地址
        host = self.request.host  # 获取请求的主机地址
        result="ip地址为%s,host为%s"%(ip,host)
        return self.write(result)


if __name__ == '__main__':
    app = Application([
        ("/request", DetailHandler)
    ])
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Browser input: http://localhost:8888/request

The page displays:

ip地址为::1,host为localhost:8888

The commonly used httputil.HTTPServerRequest object attributes are as follows:

##cookiesCookies dictionary submitted by the client
Attribute name Description
method HTTP request method, such as: GET, POST
uri The uri requested by the client Complete content.
path uri path name, that is, does not contain the query string
query uri The query string of
version is the HTTP version used when the client sends the request, for example: HTTP/1.1
headers Return HTTP Headers in the form of a dictionary
body Return the HTTP message body in the form of a string
remote_ip Client’s IP address
protocol Request protocol, such as: HTTP, HTTPS
host The host name of the request message
arguments All parameters submitted by the client.
files Return the files uploaded by the client in the form of a dictionary, each file name corresponds to an HTTPFile
#

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

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more