bugnet17
bugnet17

Reputation: 181

Generate multiple HTTP request from list values

I have a list with three hashes and the script should create HTTP request for each hash in the list and then save the file of the hash. for some reason, the script generates only one HTTP request and for this reason, I manage to download only one file instead of three.

all_hashes = ['07a3355f81f0dbd9f5a9a', 'e0f1d8jj3d613ad5ebda6d', 'dsafhghhhffdsfdd']
for hash in all_hashes:
    params = {'apikey': 'xxxxxxxxxxxxx', 'hash': (hash)}
    response = requests.get('https://www.test.com/file/download', params=params)
    downloaded_file = response.content

name = response.headers['x-goog-generation']

if response.status_code == 200:
        with open('%s.bin' % name, 'wb') as f:
                f.write(response.content)

Upvotes: 0

Views: 13

Answers (1)

Uku Loskit
Uku Loskit

Reputation: 42030

your response checking and saving code should also be in the loop e.g

all_hashes = ['07a3355f81f0dbd9f5a9a', 'e0f1d8jj3d613ad5ebda6d', 'dsafhghhhffdsfdd']
for hash in all_hashes:
    params = {'apikey': 'xxxxxxxxxxxxx', 'hash': (hash)}
    response = requests.get('https://www.test.com/file/download', params=params)
    downloaded_file = response.content

    name = response.headers['x-goog-generation']

    if response.status_code == 200:
            with open('%s.bin' % name, 'wb') as f:
                    f.write(response.content)

currently you response is the last request since your code is executed after the loop is finished.

Upvotes: 1

Related Questions