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

write a function sumsquares(1) that takes as input a list of integers and return the sum of all the perfect squares in 1

For an expression with parentheses, we can define the nesting depth as the maximum number of parentheses that are open when reading the expression from left to right. For instance, the nesting depth of "(33+77)(44-12)" is 1, while the nesting depth of "(a(b+c)-d)(e+f)" is 2.

Write a Python function depth(s) that takes a string containing an expression with parentheses and returns an integer, the nesting depth of s. You can assume that s is well-parenthesized: that is, that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it.

A positive integer n is said to be perfect if the sum of the factors of n, other than n itself, add up to n. For instance 6 is perfect since the factors of 6 are {1,2,3,6} and 1+2+3=6. Likewise, 28 is perfect because the factors of 28 are {1,2,4,7,14,28} and 1+2+4+7+14=28.

Write a Python function perfect(n) that takes a positive integer argument and returns True if the integer is perfect, and False otherwise.

please reply for these questions

please give solutions for the above questions please ..

atleast for one question

write a function sumsquares(1) that takes as input a list of integers and return the sum of all the perfect squares in 1

For an expression with parentheses, we can define the nesting depth as the maximum number of parentheses that are open when reading the expression from left to right. For instance, the nesting depth of "(33+77)(44-12)" is 1, while the nesting depth of "(a(b+c)-d)(e+f)" is 2.

Write a Python function depth(s) that takes a string containing an expression with parentheses and returns an integer, the nesting depth of s. You can assume that s is well-parenthesized: that is, that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.