Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Client/Server socket send user input

11 views
Skip to first unread message

jas

unread,
Oct 25, 2005, 12:01:31 PM10/25/05
to
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.

jas

unread,
Oct 25, 2005, 3:39:06 PM10/25/05
to
I even tried inserting a "\r" or "\r\n" or "\n" to stdout, also tried
the same using msvcrt.putch() ...but no luck. I still have to hit
enter to get the prompt , where I can then type a command and hit
enter.

For example, I get this displayed:
[example]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>[cursor_is_here]
[/example]

...so I type something like.."ver" but I get

[example]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'ver' is not defined
>>>
[/example]

Now, if I start over and have...

[example]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>[cursor_is_here]
[/example]

..and I press the Enter key I end up with

[example]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>

>>>[cursor_is_here]
[/example]

So, I get the prompt, I hit enter, which then displays the ">>>" ..and
if I type "ver" there, I get back what I expect.

I just wish I didn't have to hit enter before being able to type a
command.

Message has been deleted

jas

unread,
Oct 26, 2005, 7:49:20 AM10/26/05
to
Dennis,
Thanks. That certainly looks like it could work. I understand
about the interactive shell and my app going back and forth with the
reads/writes. When my program runs it won't be used in an interactive
python shell, but that is the only way I know of to really test it.

Message has been deleted
0 new messages