URLLIB3 is a powerful, well-organized Python library for HTTP clients, and many Python native systems have started using URLLIB3. URLLIB3 provides a number of important features that are not available in the Python standard library:
1. Thread Safety
2. Connection Pool
3, client SSL/TLS verification
4, File Division code upload
5. Assist in handling duplicate requests and HTTP relocation
6, support compression coding
7. Support HTTP and Socks proxy
8.100% Test Coverage
The URLLIB3 function is very powerful, but it is very simple to use:
Installation:
URLLIB3 can be installed by PIP:
$pip Install URLLIB3
You can also download the latest source code on GitHub and install it after unpacking:
$git Clone Git://github.com/shazow/urllib3.git
$python setup.py Install
Use of URLLIB3:
Generate requests (Request):
First, you must import the URLLIB3 module:
Then you need a Poolmanager instance to generate the request, which handles the connection to the thread pool and all the details of thread safety without any human action:
Create a request through the Ask () method:
The request () method returns a HttpResponse object.
You can also use the request () method to add some additional information to requests, such as:
The data items in the request (requests) can include:
Headers:
In the request () method, you can define a dictionary type (dictionary) and pass in as the headers parameter:
Query Parameters:
For, and delete requests, you can simply pass in by defining a dictionary type as the fields parameter:
For Post and put requests (request), the incoming data needs to be manually encoded and then added after the URL:
Form Data:
For put and post requests (request), URLLIB3 automatically encodes the field parameter of the dictionary type into a table type.
Json:
When initiating a request, you can send a compiled JSON data by defining the body parameter and defining the headers Content-type parameter:
Files & Binary data:
Uploading a file using Multipart/form-data encoding can be done using the same method as passing in the form data data and defining the file as a tuple (File_name,file_data):
The definition of file name (filename) is not strictly required, but is recommended to make it behave more like a browser. You can also add another data to the tuple to define the MIME type of the file:
If you are sending raw binary data, simply define it as the body parameter. It is also recommended to set the Content-type parameter of the header:
Timeout:
With timeout, you can control when the request runs. In some simple applications, you can set the timeout parameter to a floating-point number:
For finer control, you can use a timeout instance to set the connection timeout and the read timeout separately:
If you want all the request to follow a timeout, you can define the timeout parameter in Poolmanager:
Or
Timeout on the Poolmanager level is overwritten when timeout is defined again in the specific request.
Request retry (retrying requests):
URLLIB3 can automatically retry the idempotent request, the same principle as the handles redirect. The retry can be controlled by setting the retries parameter. The URLLIB3 defaults to 3 request retries and 3 direction changes.
Define an integral type for the retries parameter to change the number of retry requests:
Turn off request retry (retrying requests) and redirect (redirect) as long as the retries is defined as false:
Turn off redirection (redirect) but leave the retry (retrying request), and define the redirect parameter as false:
For finer control, you can use the retry instance, which allows finer control over the retry of the request.
For example, 3 requests are retried, but only 2 redirects are made:
If you want all requests to follow a retry policy, you can define retry parameters in Poolmanager:
Or
When retry is defined again in a specific request, the retry on the Poolmanager level is overwritten.
This article is referenced from: URLLIB3
PYTHON--URLLIB3 Library Detailed 1