-1
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 ?

  • chunk-size==8 is not resizing the image any solution ? –  Sep 10 '22 at 10:32
  • 1
    `chunk_size=8` this is the correct syntax. And how would it resize the image? You can refer to this question: https://stackoverflow.com/questions/46205586/why-to-use-iter-content-and-chunk-size-in-python-requests – GodWin1100 Sep 10 '22 at 10:34
  • bytes=0-11 this is the original size of the image chunk-size should resize the image to 8 bytes but it doesn't work –  Sep 10 '22 at 10:36
  • just to make sure i understand : by leaving stream=true and chunk=1mb this means the requests library will take every 1 mb from the file and put it in my computer am i right ? and if i set stream=false means requests will not take any chunk size of the file i will have to wait till it completes the size of the file to put it in my computer right ???? –  Sep 10 '22 at 10:54
  • Yes logically @Mr.spook – GodWin1100 Sep 10 '22 at 11:38

1 Answers1

0

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)
Timeless
  • 22,580
  • 4
  • 12
  • 30
  • does it matter if i use iter_content(chunk_size=) because stream=true now ? –  Sep 10 '22 at 10:57