1

After a few tentatives and seeing a lot of examples and questions around here I can't figure out why I'm not able to download a file using requests module, the File i'm trying to download is around 10mb only:

try:
    r = requests.get('http://localhost/sample_test', auth=('theuser', 'thepass'), stream=True)
    with open('/tmp/aaaaaa', 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            f.write(chunk)
except:
    raise

Empty file:

[xxx@xxx ~]$ ls -ltra /tmp/aaaaaa 
-rw-rw-r--. 1 xxx xxx 0 Jul 21 12:38 /tmp/aaaaaa

EDIT: I just discovered that it's necessary to authenticate into the API with session instead basic authentication, that information wasn't available on the specification. The code above works properly. I voted to close this question.

thclpr
  • 5,778
  • 10
  • 54
  • 87
  • see this https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests – efirvida Jul 21 '17 at 11:58
  • Are you able to download that file from a browser without any issues ? Also, checking the response headers won't hurt. – Himal Jul 21 '17 at 11:59
  • @Himal I just discovered that I have to start a session with authentication in order to use the api to download the file. I'm going to open another question and update this one. – thclpr Jul 21 '17 at 12:17
  • I'm voting to close this question as off-topic because the code works, it was a speficiation error given by the client that induced me on error. – thclpr Jul 21 '17 at 12:20

2 Answers2

3

From the answers here try something like

import requests

url = 'http://localhost/sample_test'
filename = '/tmp/aaaaaa'
r = requests.get(url, auth=('theuser', 'thepass'), stream=True)

if r.status_code == 200:
    with open(filename, 'wb') as f:
        f.write(r.content)
efirvida
  • 4,592
  • 3
  • 42
  • 68
0

I'm adding the solution to my problem here, in case that anyone needs it:

import requests

auth = 'http://localhost/api/login'
payload = {'username': 'the_user', 'password': 'the_password'}

with requests.Session() as session:
    r = session.post(auth, data=payload)
    if r.status_code == 200:
        print('downloading')
        get = session.get('http://localhost/sample_test', stream=True)
        if get.status_code == 200:
            with open('/tmp/aaaaaa', 'wb') as f:
                for chunk in get.iter_content(chunk_size=1024):
                    f.write(chunk)
    else:
        print r.status_code
thclpr
  • 5,778
  • 10
  • 54
  • 87