Search code examples
pythonrequeststream

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)
req.raise_for_status()
with open('Forest.jpg', 'wb') as fd:
  for chunk in req.iter_content(chunk_size=50000):
    print('Received a Chunk')
    fd.write(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.


Solution

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

    import requests
    
    resp = requests.get("https://www.google.com", stream=True)
    print(resp.headers["Set-Cookie"])
    

    Prints:

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