使用 Python 自动化流 API?

Unn*_*kla 4 python api json python-requests

如何使用 Python 使用来自 Streaming API 的响应分块数据。

尝试使用“请求”模块,但在向 API 发送请求后,python 脚本挂起,没有通过控制台写入响应。

mha*_*wke 7

您需要使用“流请求”

调用时设置stream参数。请求不会阻塞,您可以使用对象的方法迭代流数据:Truerequests.get()iter_content()Response

response = requests.get('http://server/stream-forever', stream=True)
for data in response.iter_content(chunk_size=10):
    print data
Run Code Online (Sandbox Code Playgroud)

这将以 10 个字节为单位读取响应内容并将其打印到控制台。