title: How to download a file with urllib3?
body: <p>This is based on another question on this site: <a href="https://stackoverflow.com/questions/17285464/whats-the-best-way-to-download-file-using-urllib3">What&#39;s the best way to download file using urllib3</a>
  However, I cannot comment there so I ask another question:</p>
  
  <p>How to download a (larger) file with urllib3?</p>
  
  <p>I tried to use the same code that works with urllib2 (<a href="https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3">Download file from web in Python 3</a>), but it fails with urllib3:</p>
  
  <pre><code>http = urllib3.PoolManager()
  
  with http.request('GET', url) as r, open(path, 'wb') as out_file:       
      #shutil.copyfileobj(r.data, out_file) # this writes a zero file
      shutil.copyfileobj(r.data, out_file)
  </code></pre>
  
  <p>This says that 'bytes' object has no attribute 'read'</p>
  
  <p>I then tried to use the code in that question but it gets stuck in an infinite loop because data is always '0':</p>
  
  <pre><code>http = urllib3.PoolManager()
  r = http.request('GET', url)
  
  with open(path, 'wb') as out:
      while True:
          data = r.read(4096)         
          if data is None:
              break
          out.write(data)
  r.release_conn()
  </code></pre>
  
  <p>However, if I read everything in memory, the file gets downloaded correctly:</p>
  
  <pre><code>http = urllib3.PoolManager()
  r = http.request('GET', url)
  with open(path, 'wb') as out:
      out.write(data)
  </code></pre>
  
  <p>I do not want to do this, as I might potentially download very large files.
  It is unfortunate that the urllib documentation does not cover the best practice in this topic.</p>
  
  <p>(Also, please do not suggest requests or urllib2, because they are not flexible enough when it comes to self-signed certificates.)</p>