Search code examples
pythonproxyconnection-poolingurllib3

How to set proxy while using urllib3.PoolManager in python


I am currently using connection pool provided by urllib3 in python like the following,

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

However, I don't know how to set proxy while using this connection pool. I tried to set proxy in the 'request' like pool.request('GET', 'http://example.com', proxies={'http': '123.123.123.123:8888'} but it didn't work.

Can someone tell me how to set the proxy while using connection pool

Thanks~


Solution

  • There is an example for how to use a proxy with urllib3 in the Advanced Usage section of the documentation. I adapted it to fit your example:

    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()
    

    The ProxyManager behaves the same way as a PoolManager would.