diff options
| author | Ryan Williams <breath@alum.mit.edu> | 2010-03-31 13:14:04 -0700 |
|---|---|---|
| committer | Ryan Williams <breath@alum.mit.edu> | 2010-03-31 13:14:04 -0700 |
| commit | 2d0dc9a269300dcc077674489839e858739ca116 (patch) | |
| tree | 2a781a60aea2c13c4461582a8bd3a8a399eab5b3 /examples | |
| parent | 737e2e71aed0993b2e73a69e6f6b12112c0a7c77 (diff) | |
| download | eventlet-2d0dc9a269300dcc077674489839e858739ca116.tar.gz | |
Updated chat server example to be resilient against broken pipes. Also used a set instead of a list and parameterized the port.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/chat_server.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/examples/chat_server.py b/examples/chat_server.py index bf24c98..3577e98 100644 --- a/examples/chat_server.py +++ b/examples/chat_server.py @@ -1,27 +1,35 @@ import eventlet +from eventlet.green import socket -participants = [] +PORT=3001 +participants = set() def read_chat_forever(writer, reader): line = reader.readline() while line: print "Chat:", line.strip() for p in participants: - if p is not writer: # Don't echo - p.write(line) - p.flush() + try: + if p is not writer: # Don't echo + p.write(line) + p.flush() + except socket.error, e: + # ignore broken pipes, they just mean the participant + # closed its connection already + if e[0] != 32: + raise line = reader.readline() participants.remove(writer) print "Participant left chat." try: - print "ChatServer starting up on port 3000" - server = eventlet.listen(('0.0.0.0', 3000)) + print "ChatServer starting up on port %s" % PORT + server = eventlet.listen(('0.0.0.0', PORT)) while True: new_connection, address = server.accept() print "Participant joined chat." new_writer = new_connection.makefile('w') - participants.append(new_writer) + participants.add(new_writer) eventlet.spawn_n(read_chat_forever, new_writer, new_connection.makefile('r')) |
