Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2011, 05:59 AM   PM User | #1
breaksand30
New to the CF scene

 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
breaksand30 is an unknown quantity at this point
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)
breaksand30 is offline   Reply With Quote
Old 01-25-2011, 10:27 AM   PM User | #2
Kakao
Regular Coder

 
Join Date: Mar 2006
Location: Brasília, Brazil
Posts: 153
Thanks: 0
Thanked 0 Times in 0 Posts
Kakao is on a distinguished road
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.
Kakao is offline   Reply With Quote
Reply

Bookmarks

Tags
client, python, remote, server, socket

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:46 PM.


Advertisement
Log in to turn off these ads.