How to print Python requests response in real time?
Sep 14, 1:58pm

To print the response from a Python requests call in real time—especially useful for large responses or streaming data—you can use the stream parameter. This allows you to process the response as it arrives, rather than waiting for the entire response to download.

Example: Streaming and Printing Response in Real Time

python
import requests

url = "https://example.com/large-file"  # Replace with your target URL
response = requests.get(url, stream=True)

# Check if the request was successful
if response.status_code == 200:
    for chunk in response.iter_content(chunk_size=8192):
        if chunk:  # Filter out keep-alive chunks
            print(chunk.decode('utf-8'), end='', flush=True)
else:
    print(f"Request failed with status code: {response.status_code}")

Key Points:

  • stream=True: Ensures the response is streamed rather than downloaded all at once.
  • iter_content(chunk_size=...): Reads the response in chunks (e.g., 8KB at a time).
  • decode('utf-8'): Converts bytes to a string (adjust encoding if needed).
  • flush=True: Forces the output to be printed immediately.

For Text Responses (e.g., APIs)

If the response is JSON or text, you can also use iter_lines():

python
response = requests.get(url, stream=True)
for line in response.iter_lines():
    if line:  # Filter out empty lines
        print(line.decode('utf-8'))

Use Cases

  • Downloading large files (e.g., videos, logs).
  • Processing real-time API responses (e.g., Twitter streams, WebSockets).
  • Monitoring progress during downloads.
Sep 14, 1:58pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more