The chunk-size is not working in python requests

Question:

Import requests
Headers = {'range':'bytes=0-11'}
rrequests = requests.get('https://httpbin.org/image/webp', stream=True, headers=headers)

with open('testfile.jpg', 'wb') as file:
    For i in rrequests.iter_content(chunk-size==8):
        write_file = file.write(i)
        print(write_file)

result :

`name 'chunk' is not defined`

Why do i get name error chunk is not defined ?

Asked By: Mr.spook

||

Answers:

You’re not using the correct syntax for iter_content().

Try this instead :

import requests

headers = {'range':'bytes=0-11'}
rrequests = requests.get('https://httpbin.org/image/webp', stream=True, headers=headers)

with open('testfile.jpg', 'wb') as file:
    for i in rrequests.iter_content(chunk_size=8):
        write_file = file.write(i)
        print(write_file)
Answered By: L'Artiste
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.