从列表值生成多个HTTP请求

时间:2018-11-05 13:27:42

标签: python

我有一个带有三个哈希的列表,脚本应该为列表中的每个哈希创建HTTP请求,然后保存哈希文件。 由于某种原因,该脚本仅生成一个HTTP请求,因此,我设法仅下载一个文件,而不是三个文件。

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)

1 个答案:

答案 0 :(得分:1)

您的响应检查和保存代码也应该在循环中 例如

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)

由于您的代码是在循环完成后执行的,因此当前response last 请求。