Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - execute command and get output

Tags:

python

cmd

I need to build a server that can execute a command line command and send back the output of the command

Example:

for the command- echo hello world, the server will return the string "hello world".

I tried to use subprocess.call() function but it returns a number and not a string. I have the sever ready, i just need this this.

Code:

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)
like image 968
guy ribak Avatar asked Dec 15 '15 09:12

guy ribak


1 Answers

How about using subprocess.check_output instead? From the manual: "Run command with arguments and return its output as a byte string."

It would be something like data = subprocess.check_output(client_data, shell=True) then.

See this man page for more information.

like image 197
MrHug Avatar answered Oct 19 '22 23:10

MrHug