Files
deb-python-eventlet/examples/twisted_basic_server.py
Denis Bilenko 597a2c0914 renamed twistedutil/basic.py to twistedutil/protocol.py;
removed listenXXX shortcuts: use reactor.listenXXX+SpawnFactory;
added buffer_class class attribute to SpawnFactory;
twistedutil: converted class names to CamelCase;
moved LineOnlyReceiverBuffer to twistedutil.protocols.basic;
fixed the examples.
2008-11-04 23:31:54 +06:00

31 lines
968 B
Python

from eventlet.twistedutil import join_reactor
from eventlet.twistedutil.protocol import SpawnFactory
from eventlet.twistedutil.protocols.basic import LineOnlyReceiverBuffer
class Chat:
def __init__(self):
self.participants = []
def handler(self, conn):
peer = conn.getPeer()
print 'new connection from %s' % (peer, )
self.participants.append(conn)
try:
for line in conn:
print 'received from %s: %s' % (peer, line)
for buddy in self.participants:
if buddy is not conn:
buddy.sendline('from %s: %s' % (peer, line))
except Exception, ex:
print peer, ex
else:
print peer, 'connection done'
finally:
self.participants.remove(conn)
chat = Chat()
from twisted.internet import reactor
reactor.listenTCP(8007, SpawnFactory(chat.handler, LineOnlyReceiverBuffer))
reactor.run()