Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python telnet server -- Server not sending back output to client

Status
Not open for further replies.

khanza

Technical User
Feb 20, 2003
70
US
So I'm trying to make a telnet like application.

So far, I've established a connection between the server and the client app, and I've used the subprocess module to pass commands from the client (sent to the server) to bash.

Here's my server code -
Code:
#! /usr/bin/env python
import socket,sys,subprocess
HOST = ''
PORT = 335
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while 1:
    try:
        conn, addr = s.accept()
        data = conn.recv(1024)
        a = data
        b = a.split()
        print ''
        retcode = subprocess.call(b)
    except sock.error:
        s.close()
    if a == "exit":
        print "Client is disconnecting"

Client code -
Code:
#! /usr/bin/env python
import socket,sys
i = raw_input("IP: ")
p = 335
while 1:
        try:
                d = raw_input("Command: ")
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((i, p))
                s.send(d)
        except socket.error:
                print "Error"
                break

So the problem here, is that the client sends the cmd's to bash on the server, and the output appears on the server instead of the client.
So I need to send the output BACK to the client and print it.


When I tried to solve this issue, this is the code I ended up with -
Server -
Code:
#! /usr/bin/env python
import socket,sys,subprocess
HOST = ''
PORT = 335
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while 1:
    conn, addr = s.accept()
    data = conn.recv(1024)
    a = data
    b = a.split()
    print ''
    retcode = subprocess.call(b)
    string = str(retcode)
    s.send(string)
    if a == "exit":
        print "Client is disconnecting"

Client -
Code:
#! /usr/bin/env python
import socket,sys
i = raw_input("IP: ")
p = 335
while 1:
        try:
                d = raw_input("Command: ")
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((i, p))
                s.send(d)
                conn, addr = s.accept()
                output = conn.recv(1024)
                prt = int(output)
                print prt
        except socket.error:
                print "Error"
                break

This doesn't work either....

Any help?

Thanks!
khan
 
You have several problems.

First, you need to capture the command's stdout (and probably stderr), not just its return code. Read the subprocess documentation for how. ("stdin, stdout, and stderr specify what the subprocess's input, output, and error streams will be. You can provide a file object or a file descriptor, or you can use the constant subprocess.PIPE to create a pipe between the subprocess and the parent.")

Second, don't accept() on the client, just use s.recv.


a python blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top