This article is from NetEase Cloud community
Author: Wang Tao
The optional parameters are introduced one by one:
parameter | paraphrase | The sample |
params | Generate url? Key=value | Example 1:
Example 2:
|
data | Supports dictionaries, lists, and strings. The POST method is used to simulate an HTML form |
Example 1:
Example 2:
|
json | Post is used to pass JSON data to the server, Many Ajax requests are passed JSON |
Example 1:
The captured header is content-Type: application/json |
headers | A custom HTTP header that is merged with the request’s own default header as the request header. Note: All header values must be String, byteString, or Unicode. |
Example 1:
|
cookies | You can access the cookie in the reply through cookies, You can also send custom cookies to the server. With custom cookie objects, you can also specify properties such as valid fields |
Example 1: Get the cookie in the reply
Example 2: Sending cookies to the server
|
files | Upload a multipart-encoded file | Example 1: Upload a file
Example 2: Explicitly set the file name, file type, and request header
|
auth | Support multiple HTTPBasicAuth HTTPDigestAuth/HTTPProxyAuth kind of certification | Example 1
After packet capture, the HTTP header is as follows:
|
timeout | 1 float: indicates the connection waiting time. This parameter is valid only for socket connection. 2 tuple:(connect timeout, read timeout) | Example 1:
|
allow_redirects | If you use GET, OPTIONS, POST, PUT, PATCH, or DELETE, You can disable redirect processing with the allow_redirects parameter. Note: In the crawler process, we need to disable the jump in some scenarios and set it to False. The default True |
Example 1: When 3xx is received, the system automatically jumps to:
|
proxies | Configuring proxy information, nothing to say. Configure HTTP and HTTPS proxy as required | Example 1:
|
stream | Set stream to true if the reply is streaming. Note: Active shutdown is required, otherwise the connection will not be released | Example 1: Download baidu pictures:
|
verify | The default value is True to verify the server certificate. If it is a string, it is the path of CA_BUNDLE, which is the certificate path. If you can’t find the certificate, use PEM from Fiddler as in Example 2, or install the Certifi package (which comes with a set of trusted root certificates for Requests). | Example 1: Disabling certificate validation (recommended)
Example 2: Borrow Fiddler’s converted PEM certificate to access Central Asia. Fiddlerroot.zip download address:
|
cert | Type: String: Represents the cert file of the SSL client (.pem) file path tuple :(‘cert’,’key’),verify given the server certificate, cert given is the client certificate (for HTTPS bidirectional authentication) | This field has not been tested and has not yet been used. If you’re interested, you can look into it |
Key functions and parameters in Tornado
Tornado Non-blocking HttpClient
Tornado has two non-blocking implementations of HttpClient, SimpleAsyncHTTPClient and CurlAsyncHTTPClient. You can call them the base class for AsyncHTTPClient, through AsyncHTTPClient. The configure method which one to choose to use the above implementation, or directly instantiate any of the above a subclass. The default is SimpleAsyncHTTPClient, which already meets the needs of most users, but we chose CurlAsyncHTTPClient with more advantages.
-
CurlAsyncHTTPClient supports more features, such as proxy Settings, specifying network outgoing interfaces, and so on
-
CurlAsyncHTTPClient is also accessible for sites that are not very compatible with HTTP,
-
CurlAsyncHTTPClient faster
-
Before Tornado 2.0, CurlAsyncHTTPClient was the default.
2 introduction of Tornado key functions and parameters
Sample code (similar to the previous) :
@gen.coroutinedef fetch_url(url):
"""Grab url"""
try:
c = CurlAsyncHTTPClient() Define an HttpClient
req = HTTPRequest(url=url) Define a request
response = yield c.fetch(req) Make a request
print response.body
IOLoop.current().stop() Stop the ioloop thread
except: print traceback.format_exc()Copy the code
As you can see, this httpClient is also very easy to use. Create an HTTPRequest and call the Fetch method of HTTPClient to initiate the request. Let’s take a look at HTTPRequest’s definition and see what key parameters we need to know.
class HTTPRequest(object):
"""HTTP client request object."""
# Default values for HTTPRequest parameters.
# Merged with the values on the request object by AsyncHTTPClient
# implementations._DEFAULTS = dict(connect_timeout=20.0, request_timeout=20.0, follow_redirects=True, max_redirects=5, decompress_response=True, proxy_password=' ',
allow_nonstandard_methods=False,
validate_cert=True)
def __init__(self, url, method="GET", headers=None, body=None,
auth_username=None, auth_password=None, auth_mode=None,
connect_timeout=None, request_timeout=None,
if_modified_since=None, follow_redirects=None,
max_redirects=None, user_agent=None, use_gzip=None,
network_interface=None, streaming_callback=None,
header_callback=None, prepare_curl_callback=None,
proxy_host=None, proxy_port=None, proxy_username=None,
proxy_password=None, proxy_auth_mode=None,
allow_nonstandard_methods=None, validate_cert=None,
ca_certs=None, allow_ipv6=None, client_key=None,
client_cert=None, body_producer=None,
expect_100_continue=False, decompress_response=None,
ssl_options=None):
r"""All parameters except ``url`` are optional.Copy the code
NetEase Cloud Free experience pavilion, 0 cost experience 20+ cloud products!
For more information about NetEase’s r&d, product and operation experience, please visit NetEase Cloud Community.
Relevant article: “recommended” to know things by learning | see, following instead the Android applications and protective techniques