我正在尝试通过 CMD 中的以下行将 cURL 的输出通过管道传输到 Python 模块的输入:
curl https://api.particle.io/v1/devices/e00fce68515bfa5f850de016/events?access_token=ae40788c6dba577144249fec95afdeadb18e6bec | pythonmodule.py
当curl单独运行时(没有“| pythonmodule.py
”),它每30秒连续传输一次数据(它通过温度和湿度传感器连接到Argon IoT)打印实时温度但是当我尝试通过 | 重定向输出时,它似乎只工作一次,它不会连续运行 python 模块,而每次提供新数据时它都应该运行。
我尝试使用库requests.get()
,但由于它是连续流,因此似乎卡住在get()
上。
有人可以解释一下这个 cURL 流实际上是如何工作的吗?
最佳答案
关于卡住请求连续流,您可以使用 requests
中的 Body Content Workflow
来避免等待整个内容立即下载:
with requests.get('your_url', stream=True) as response:
for line in response.iter_lines(decode_unicode=True):
if line:
print(line)
输出:
:ok
event: SensorVals
data: {"data":"{humidity: 30.000000, temp: 24.000000}","ttl":60,"published_at":"2019-11-28T13:53:04.592Z","coreid":"e00fce68515bfa5f850de016"}
event: SensorVals
data: {"data":"{humidity: 29.000000, temp: 24.000000}","ttl":60,"published_at":"2019-11-28T13:53:34.604Z","coreid":"e00fce68515bfa5f850de016"}
...
https://requests.readthedocs.io/en/master/user/advanced/#body-content-workflow
关于python - cURL 流作为 python 模块的标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089616/