5

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)
1

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Maybe this piece of code will help

import subprocess

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.