python requests stream iterate header only instead of body

This code I got from this site

import requests
req = requests.get('path/to/forest.jpg', stream=True)
with open('Forest.jpg', 'wb') as fd:
  for chunk in req.iter_content(chunk_size=50000):
    print('Received a Chunk')

Basically that code explains how to get the body content streamly. My case is similiar. Instead of iterating the body content, I just want to iterate the http header. Specifically, I just want to get Set-Cookie header and then terminate the connection. With doing that, I can save my download bandwidth if the body content is large while I just need the header.


  • If I understand you correctly, you can use response.headers to only get HTTP headers:

    import requests
    resp = requests.get("", stream=True)


    SOCS=ABBaBgiFrKdpBg; expires=Tue, 12-Nov-2024 10:43:27 GMT; path=/;; ...