urllib3 ConnectionPools

A connection pool is a container for a collection of connections to a specific host.
If you need to make requests to the same host repeatedly, then you should use a HTTPConnectionPool.

1 from urllib3 import HTTPConnectionPool
2 
3 
4 pool = HTTPConnectionPool('www.baidu.com', maxsize=1)
5 res = pool.request('GET', '/s', fields={'wd': 'HELLO'})
6 print(res.status)
7 print(res.data)

By default, the pool will cache just one connection. If you’re planning on using such a pool in a multithreaded
environment, you should set the maxsize of the pool to a higher number, such as the number of threads. You can
also control many other variables like timeout, blocking, and default headers.

A ConnectionPool can be used as a context manager to automatically clear the pool after usage.

with HTTPConnectionPool('www.baidu.com', maxsize=1) as pool:
    res = pool.request('GET', '/s', fields={'wd': 'HELLO'})
    print(pool.pool)

API

原文地址:https://www.cnblogs.com/shadowwalker/p/5283074.html