python-执行命令并获取输出

python-执行命令并获取输出,python,cmd,Python,Cmd,我需要构建一个能够执行命令行命令并发回命令输出的服务器 示例: 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) 对于命令-echo hello world,服务器将返回字符串“hello world” 我尝试使

我需要构建一个能够执行命令行命令并发回命令输出的服务器

示例:

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)
对于命令-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)

使用
子流程怎么样?改为检查输出
?从手册中:
“使用参数运行命令,并以字节字符串形式返回其输出。”

它类似于
data=subprocess。然后检查输出(client\u data,shell=True)


有关更多信息,请参阅。

也许这段代码会有所帮助

import subprocess

proc = subprocess.Popen('ping google.com', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
tmp = proc.stdout.read()
print tmp
请阅读