如何在 python 中使用 urllib3.PoolManager 时设置代理

How to set proxy while using urllib3.PoolManager in python

我目前正在使用 python 中 urllib3 提供的连接池,如下所示,

pool = urllib3.PoolManager(maxsize = 10)
resp = pool.request('GET', 'http://example.com')
content = resp.read()
resp.release_conn()

但是,我不知道如何在使用这个连接池时设置代理。我尝试在 'request' 中设置代理,例如 pool.request('GET', 'http://example.com', proxies={'http': '123.123.123.123:8888'},但它没有用。

谁能告诉我如何在使用连接池时设置代理

谢谢~

Advanced Usage section of the documentation 中有一个关于如何使用带有 urllib3 的代理的示例。我对其进行了调整以适合您的示例:

import urllib3
proxy = urllib3.ProxyManager('http://123.123.123.123:8888/', maxsize=10)
resp = proxy.request('GET', 'http://example.com/')
content = resp.read()
# You don't actually need to release_conn() if you're reading the full response.
# This will be a harmless no-op:
resp.release_conn()

ProxyManager 的行为方式与 PoolManager 相同。