我试图通过CMD中的以下行将cURL的输出输送到Python模块的输入:
curl https://api.particle.io/v1/devices/e00fce68515bfa5f850de016/events?access_token=ae40788c6dba577144249fec95afdeadb18e6bec | pythonmodule.py当curl自己运行时(没有"| pythonmodule.py“),它每30秒连续地流一次数据(它与带有温湿度传感器的氩IoT连接),完美地打印实时温湿度。但是,当我尝试重定向输出时,它似乎只工作了一次,它不会连续地运行output模块,在每次提供新数据的情况下,它都应该运行这个模块。
我试图使用库requests.get(),但由于它是一个连续流,它似乎冻结在get()上。
有人能解释一下这个cURL流实际上是如何工作的吗?
发布于 2019-11-28 13:43:37
关于冻结请求连续流的问题,您可以使用来自requests的requests来避免等待全部内容立即下载:
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
https://stackoverflow.com/questions/59089616
复制相似问题