Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Python server/client remote execution question

    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:
    Code:
    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:
    Code:
    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:
    Code:
    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)

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You only coded the communication part. There is no code on the server to execute the command. Look at the subprocess module to achieve the execution of commands.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •