client/server chat program

Jason Orendorff jason at jorendorff.com
Mon Dec 10 18:57:23 EST 2001


"neuromorphus" <neuromorphus at yahoo.com> wrote:
> The client program continuously asks for 'raw_input' from the user,and
> then sends the msg to the server to be broadcasted. However, I can't
> output messages to the terminal/console, if the client program is
> waiting for input.

This question is common -- and rich with implications.

The best way to proceed is to read a few chapters of Stevens'
excellent book, "Advanced Programming in the UNIX Environment".
Many deep Unix secrets and aha! moments await.
  http://www.kohala.com/start/apue.html

The answer, in summary:
 * Use select.select() to wait for input.  Provide the list
   [sys.stdin, mysocket] to select.  This is Unix-specific.
 * When select tells you that there's input waiting on stdin,
   use os.read(sys.stdin.fileno(), 1000) to get it.

Alternatively, you could use 2 threads, one for input and one
for output.

Either way, the output will typically stomp all over what the user
is currently typing.  Everything will work right, but the display
will be confusing.  (I hope it's clear what I mean by this.)
Curing that won't be easy.  You'll have to look at either the
"termios" and "tty" modules; or the "curses" module.  Both are
Unix-only.  "curses" is quite flexible.  It lets you put output
wherever you like on the console, and it provides color.

-- 
Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list