Client/Server socket send user input

jas codecraig at gmail.com
Tue Oct 25 12:01:31 EDT 2005


I have a basic client/server socket situation setup....where the server
accepts a connection and then waits for commands.

On the client side, I create a socket, connect to the server...then I
pass the socket to a class which just reads from the socket (in a
thread).

class Reader(Thread):
    def run(self):
        while 1:
            print self.sock.recv(1),

Now, when the server sends back a message, and needs input back the
reader hangs at the sock.recv line...which is fine, and expected.
However, I want to be able to type something into the console at that
point and hit enter, and have it sent to the server.

In my client class I tried something like...

class Client(Thread):
    def run(self):
        reader = Reader(self.sock)
        reader.start()
        while 1:
            x = raw_input("go>")
            self.sock.send(x)
            print "sent", x

However, in order to see "go>" ..I have to hit enter first, then type
in my command and hit enter to send.  I just want to type and hit
enter.

Any ideas/suggestions?

Thanks.




More information about the Python-list mailing list