I'm working on a project in Python that I'm having trouble with. I've asked so many people but they seem to not be able to help me :/ I'm coding a little program that executes system commands. The idea is that one machine opens the server and the other one opens the client and connects to the IP / Port. From there they can execute system commands and obtain the output. Only problem is, with the output, I get something like this:

Enter a command: ls
got: ls
received: 2
Enter a command:

I want the output of the command, and everything I try to add doesn't work.

Server.py:

import sys, socket

#socket.setdefaulttimeout(150)

host = ''               
port = 50103
BUFSIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: %s"%port)
s.listen(1)
print("Now listening...\n")

#conn = client socket


conn, addr = s.accept()

while True:
    print 'New connection from %s:%d' % (addr[0], addr[1])
    data = conn.recv(BUFSIZE)
    if not data:
        break
    elif data == 'exit':
        conn.send('\0')
    else:   
        conn.send(data)
   

def quit(connection):
    connection.close()

Client.py:

import sys
import socket

BUFSIZE = 1024

conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(('localhost', 50103))
while True:
    cmd = raw_input('Enter a command: ')
    conn.send(cmd)
    data = conn.recv(BUFSIZE)
    msglen = len(data)
    print "got: %s" % data
    print "received: %d" % msglen
    if data == '\0':
        print 'exiting...'
        sys.exit(0)

Can you tell us more details about the problem?

-Is the server receiving the command on the socket?
-Is the command really executed on the server?

Look for pipes if you want to capture the output of a command. You can capture it in a file, read it and return it to the client.

Can you tell us more details about the problem?

-Is the server receiving the command on the socket?
-Is the command really executed on the server?

Look for pipes if you want to capture the output of a command. You can capture it in a file, read it and return it to the client.

Well right now it isn't doing what I need it to do. I need to get the output of the command. I realize I didn't add code to handle system commands but I have no idea how I would do that. I've heard to use the subprocess module and I've tried a couple different things but ultimately I have no idea how I would use subprocess to send the command from the client to the server, have the server accept the command and then send the output to the client.

Hello, breaksand30.

I have found a solution to your problem:

while True:
    print 'New connection from %s:%d' % (addr[0], addr[1])
    data = conn.recv(BUFSIZE)
    if not data:
        break
    elif data == 'exit':
        conn.send('\0')
    else:
        # PAY ATTENTION HERE
        p = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE)
        conn.send(''.join([line for line in p.stdout.xreadlines()]))

You need to execute the command this way. I tested the "dir" command (with no arguments, with one argument) in Windows and it works.

Look at this page, it helped me and it may help you: http://stackoverflow.com/questions/36324/the-system-cannot-find-the-file-specified-when-invoking-subprocess-popen-in-pyt

Edited 5 Years Ago by GDICommander: n/a

This article has been dead for over six months. Start a new discussion instead.