如何将 rfcomm shell 命令的结果放入 python 中的变量?
How can I put result from rfcomm shell command in to a variable in python?
我在 python 中使用此脚本连接到蓝牙设备然后获取数据,但我想知道此 shell 命令的结果以便进行下一步工作
import os
import time
import signal
import subprocess
p = subprocess.Popen("sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1",shell=True)
(stderr,stdout) = p.communicate()
print 'stderr: [%s]' % stderr
print 'stdout: [%s]' % stdout
time.sleep(5)
while True:
print "Device is ready"
time.sleep(5)
此代码是我 运行 命令时的示例:
"sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1"
在shell中,returns:
Connected /dev/rfcomm0 to XX:XX:XX:XX:XX:XX on channel 1
Press CTRL-C for hangup
但是我怎样才能把上面的结果放在一个变量中,因为我需要知道这个命令的结果?
我在子进程中使用 stdout、stderr 但不起作用。
我正在使用 python 2.7
Python subprocess and user interaction
上面 link 大体上讨论了在变量中获取输出,但是我的问题中的问题与 rfcomm 有关,它没有将其结果放入变量中,我 运行 那些脚本和它们运行良好,但与 rfcomm 命令一起使用时不起作用
如果您使用的是 Python 3.5 或更高版本,
您可以改用 run
。这样你就可以像这样直接访问,
result = subprocess.run(["sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1"], stdout=subprocess.PIPE)
然后像这样访问你想要的,
result.stdout
如果您使用 Python 2.7,按照我链接的文档的建议,它们会将您重定向到 Older high-level API 部分。
从那里你会注意到你可以使用 check_output
result = subprocess.check_output(["sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1"])
注意,如果你想捕获错误也可以使用 stderr=subprocess.STDOUT
标志。
result = subprocess.check_output("sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1", stderr=subprocess.STDOUT, shell=True)
最后,有一个重要的不是你应该知道的,
By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
编辑
因为你的目标似乎在 运行 时得到了输出。看看这个 answer。我更喜欢链接而不是重新发明轮子。
您可能需要在返回数据之前发出 CTRL+C 命令。
发送信号并捕获异常以处理返回的内容。
import os
import time
import signal
import subprocess
stream = []
try:
p = subprocess.Popen("sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1",shell=True)
#(stderr, stdout) = p.communicate()
#print 'stderr: [%s]' % stderr
#print 'stdout: [%s]' % stdout
#time.sleep(5)
#print "Device is ready"
time.sleep(5)
os.kill(p.pid, signal.CTRL_C_EVENT)
p.wait()
except KeyboardInterrupt:
#except Exception:
for line in p.stdout: #May also be p.stderr
stream.append(line)
for x in stream:
print(x)
我在 python 中使用此脚本连接到蓝牙设备然后获取数据,但我想知道此 shell 命令的结果以便进行下一步工作
import os
import time
import signal
import subprocess
p = subprocess.Popen("sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1",shell=True)
(stderr,stdout) = p.communicate()
print 'stderr: [%s]' % stderr
print 'stdout: [%s]' % stdout
time.sleep(5)
while True:
print "Device is ready"
time.sleep(5)
此代码是我 运行 命令时的示例:
"sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1"
在shell中,returns:
Connected /dev/rfcomm0 to XX:XX:XX:XX:XX:XX on channel 1
Press CTRL-C for hangup
但是我怎样才能把上面的结果放在一个变量中,因为我需要知道这个命令的结果? 我在子进程中使用 stdout、stderr 但不起作用。 我正在使用 python 2.7
Python subprocess and user interaction
上面 link 大体上讨论了在变量中获取输出,但是我的问题中的问题与 rfcomm 有关,它没有将其结果放入变量中,我 运行 那些脚本和它们运行良好,但与 rfcomm 命令一起使用时不起作用
如果您使用的是 Python 3.5 或更高版本,
您可以改用 run
。这样你就可以像这样直接访问,
result = subprocess.run(["sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1"], stdout=subprocess.PIPE)
然后像这样访问你想要的,
result.stdout
如果您使用 Python 2.7,按照我链接的文档的建议,它们会将您重定向到 Older high-level API 部分。
从那里你会注意到你可以使用 check_output
result = subprocess.check_output(["sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1"])
注意,如果你想捕获错误也可以使用 stderr=subprocess.STDOUT
标志。
result = subprocess.check_output("sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1", stderr=subprocess.STDOUT, shell=True)
最后,有一个重要的不是你应该知道的,
By default, this function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
编辑
因为你的目标似乎在 运行 时得到了输出。看看这个 answer。我更喜欢链接而不是重新发明轮子。
您可能需要在返回数据之前发出 CTRL+C 命令。
发送信号并捕获异常以处理返回的内容。
import os
import time
import signal
import subprocess
stream = []
try:
p = subprocess.Popen("sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX:XX 1",shell=True)
#(stderr, stdout) = p.communicate()
#print 'stderr: [%s]' % stderr
#print 'stdout: [%s]' % stdout
#time.sleep(5)
#print "Device is ready"
time.sleep(5)
os.kill(p.pid, signal.CTRL_C_EVENT)
p.wait()
except KeyboardInterrupt:
#except Exception:
for line in p.stdout: #May also be p.stderr
stream.append(line)
for x in stream:
print(x)