码迷,mamicode.com
首页 > Web开发 > 详细

urllib3 ConnectionPools

时间:2016-03-16 14:02:41      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享

 

urllib3 ConnectionPools

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!