running.t running.t - 1 year ago 494
Python Question

What's the best way to download file using urllib3

I would like to download file over

protocol using
I have managed to do this using following code:

url = 'http://url_to_a_file'
connection_pool = urllib3.PoolManager()
resp = connection_pool.request('GET',url )
f = open(filename, 'wb')

But I was wondering what is the proper way of doing this.
For example will it work well for big files and If no what to do to make this code more bug tolerant and scalable.

Note. It is important to me to use
library not
for example, because I want my code to be thread safe.

Answer Source

Your code snippet is close. Two things worth noting:

  1. If you're using, it will consume the entire response and return the connection (you don't need to resp.release_conn() manually). This is fine if you're cool with holding the data in-memory.

  2. You could use which will stream the response, but the connection will need to be returned via resp.release_conn().

This would look something like...

import urllib3
http = urllib3.PoolManager()
r = http.request('GET', url, preload_content=False)

with open(path, 'wb') as out:
    while True:
        data =
        if not data:


The documentation might be a bit lacking on this scenario. If anyone is interested in making a pull-request to improve the urllib3 documentation, that would be greatly appreciated. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download