简体   繁体   中英

'requests' in python is not downloading file completely

I am using requests library of python to download a file of size approx. 40mb. But with my code i am getting file of size 14mb only. It is not showing any error(few warnings though before download file).

here is my code:

import requests
file_url = "https://file_url.tar"

user='username'
passw='password'
r = requests.get(file_url,auth=(user,passw),verify=False, stream = True)
with open("c.tar","wb") as code:
   for chunk in r.iter_content(chunk_size=1024):
       if chunk:
         code.write(chunk)

I tried using without 'stream=True' too. but that also not working.

When i am puting this URL in browser i am getting complete file of 40 mb.

I tried this script on some other machine and it is working fine there(and i am getting those warnings here too).

These are the warnings i am getting:

SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning . SNIMissingWarning

InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning . InsecurePlatformWarning

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning)

but i don't think there is a problem because of this because if i am running this script on some other system i am getting these warnings but script is working fine.

I am using urllib instead of requests

import urllib
url = "http://62.138.7.10/downloads/Bh2g2m.Bh2g.06.DR.M0viesC0unter.mp4?st=6MVZyTUL7X22v7ILOtB2XA&e=1502823147"
file_name = 'trial_video.mp4'
response = urllib.urlopen(url)
with open(file_name,'wb') as f:
    f.write(response.read())

Hope this will help you

I have experienced similar problems with Requests. Requests is great for doing fancy JSON api POST requests etc, but for ordinary file downloads, pycurl is a much better tool. The complicated dependency on libcurl means you shouldn't try installing pycurl with pip; instead you need to download a copy from your distro, or use one of the prebuilt win32 modules from their site.

For what it's worth, when I was using requests for file downloads, I also set up logging, and I got some "broken pipe" errors. Maybe Requests disconnects early for performance reasons or something? I didn't have the patience to figure it out when I knew there was an alternative solution that works reliably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM