获取和使用远程运行的脚本的输出子流程检查输出

2024-04-25 10:24:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我费了好大劲才弄明白如何用词来形容我的题目,这也许不是最好的描述。但下面我将详细解释我的情况。如果有什么建议,我很乐意编辑标题。你知道吗

我现在有两个树莓派。以后还会有更多。pia是运行代码和收集温度值的主机。pib只是用来运行传感器并收集温度和湿度值。你知道吗

我正在尝试将每个脚本都放在pia中,并使用ssh在其他机器上远程运行它们。你知道吗

我正在尝试一个新的东西,所以我会把两个简单的代码,我现在正在工作。你知道吗

第一个脚本是对焦. 它存储在Pi A中,但将在Pi B中运行

#!/usr/bin/env python

import Adafruit_DHT as dht
h, t = dht.read_retry(dht.DHT22, 4)

print('{0:0.1f} {1:0.1f}'.format(t, h))

输出为:

pi@raspberrypi:~/Temp_Codes $ python af.py
26.1 22.7
pi@raspberrypi:~/Temp_Codes $ 

第二个是afvar.py公司. 在这个脚本中,我让pib运行对焦但问题是,我希望能够直接得到pib的传感器的值或输出,这样我就可以继续使用它们afvar.py公司你知道吗

#!/usr/bin/env python

import subprocess

#Here I am trying to get the temperature and humidity value inside these two variables t2 and h2

t2, h2 = subprocess.check_output("sshpass -p 'x' ssh pi@192.168.x.x python < /home/pi/tempLog/af.py", shell = True)

#Some other stuff using t2 and h2 .....
#like print "temp is %f and hum is %f" % (t2, h2)

现在它给了我这样一个错误:

Traceback (most recent call last):
  File "afvar.py", line 16, in <module>
    t2, h2 = subprocess.check_output("sshpass -p 'x' ssh pi@192.168.x.x python < /home/pi/tempLog/af.py", shell = True)
ValueError: too many values to unpack

我想做的事可能吗?我一直在检查互联网,并尝试不同的解决方案,但这是我目前的困境。你知道吗


Tags: and代码py脚本pih2温度ssh
1条回答
网友
1楼 · 发布于 2024-04-25 10:24:46

subprocess.check_output返回bytes。 您想要在那里分割的,可能是您的输出'{0:0.1f} {1:0.1f}'.format(t, h)

因此,您首先必须将bytes解码为str(并可能从后面的换行符中去掉它),然后拆分它。你知道吗

output = subprocess.check_output("sshpass -p 'x' ssh pi@192.168.x.x python < /home/pi/tempLog/af.py", shell = True)
output = output.decode().strip()
t2, h2 = output.split()

因为你可能想要温度和湿度作为浮动,最后分析它们:

t2, h2 = float(t2), float(h2)

相关问题 更多 >