def write_response_to_filepath_or_buffer(filepath_or_buffer, response):
"""
Writes the response content to the filepath or buffer.
"""
if hasattr(filepath_or_buffer, "write"):
if six.PY3 and filepath_or_buffer is sys.stdout:
# Write bytes to stdout (https://stackoverflow.com/a/23932488)
filepath_or_buffer = filepath_or_buffer.buffer
mode = getattr(filepath_or_buffer, "mode", "w")
for chunk in response.iter_content(chunk_size=1024):
if chunk:
if "b" not in mode and six.PY3:
chunk = chunk.decode("utf-8")
filepath_or_buffer.write(chunk)
if filepath_or_buffer.seekable():
filepath_or_buffer.seek(0)
else:
with open(filepath_or_buffer, "wb") as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
评论列表
文章目录