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.
pythonimport 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}")
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.If the response is JSON or text, you can also use iter_lines():
pythonresponse = requests.get(url, stream=True) for line in response.iter_lines(): if line: # Filter out empty lines print(line.decode('utf-8'))
Le Chat can make mistakes. Check answers. Learn more