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.

 from urllib3 import HTTPConnectionPool

 pool = HTTPConnectionPool('www.baidu.com', maxsize=1)
res = pool.request('GET', '/s', fields={'wd': 'HELLO'})
print(res.status)
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

urllib3 ConnectionPools

上一篇:java实验一实验报告


下一篇:[转载] tcp那些事2