[英]How to execute shell command get the output and pwd after the command in Python
[英]python - execute command and get output
我需要构建一个可以执行命令行命令并发送回命令输出的服务器
例:
对于command-echo hello world,服务器将返回字符串“hello world”。
我尝试使用subprocess.call()
函数,但它返回一个数字而不是字符串。 我准备好了服务器,我只需要这个。
码:
type=struct.pack("B",2) #packing type
data=subprocess.call(client_data, shell=True)
length=struct.pack("H",len(data)) #packing lenght
client_soc.send(type+length+data)
如何使用subprocess.check_output
呢? 从手册:“使用参数运行命令并将其输出作为字节字符串返回。”
那就像data = subprocess.check_output(client_data, shell=True)
。
有关更多信息,请参见此手册页 。
也许这段代码会有所帮助
import subprocess
proc = subprocess.Popen('ping google.com', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
tmp = proc.stdout.read()
print tmp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.