我是 python 和 Linux 新手。我正在尝试从 python 执行 bash 命令,该命令向 localhost 发出命令。
最初我尝试使用 os.system
os.system('echo "(primitive-load \"/opt/doc/test.txt\")" |nc localhost xxxxx')
我已经使用 os.system 成功调用了多个命令。但这个带有文件路径的特定命令不会执行。所以我尝试使用 subprocess.call
subprocess.call( 'echo "(primitive-load \"/opt/doc/test.txt\")" |nc localhost xxxxx')
但是抛出了以下错误。
File "/usr/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我也尝试过以下方法
command = 'echo "(primitive-load \"/opt/doc/test.scm\")"|nc localhost xxxxx;'
process = os.popen(command)
还有其他方法可以实现这一目标吗?
最佳答案
尝试以下方法也许对您的情况有所帮助:
import os
command = 'echo "test"'
process = os.popen(command)
output = str(process.read())
print output
希望对你有帮助,别忘了点赞
关于python - 通过 subprocess.call 向 localhost 发出命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40545139/