PYTHON--URLLIB3 Library Detailed 1
URLLIB3 is a powerful, well-organized Python library for HTTP clients , many python Native systems have begun to use 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 GET,HEAD , and DELETE requests, you can simply define a dictionary type as fields Parameters can be passed in:
For POST and PUT requests (request), the incoming data needs to be manually encoded and then added to the URL after:
Form Data:
For put and POST requests (request), URLLIB3 automatically converts the dictionary type field parameter encoded into a table type .
JSON:
When a request is initiated , You can define the body parameter and define The headers content-type parameter to send a JSON data that has been compiled:
Files & binary data:
uploading files using multipart/form-data encoding can be done using the same method as passing in the form data , and defines the file as a tuple form (file_name,file_data):
file name (filename) are not strictly defined. , However, it 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 request to follow a timeout, you can define the timeout parameter in the In Poolmanager :
Or
Timeout on 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 retry (retrying request) and redirect (redirect) as long as the retries is defined as False to :
Turn off redirection (redirect) but remain retried (retrying request), redirect parameter is defined 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