[Twisted-Python] Twisted lineReceived (sender)
Lee Connell
lee.a.connell at gmail.com
Fri Jan 12 23:24:41 EST 2007
I just started with twisted and want to write a simple chat server. This
code here works fine, however I want to know who the data is coming from in
the callbacks or events. You can see where I commented code out in def
lineReceived as to what I want to do. How can this be done in the easiest
way?
Thanks!
#!/usr/bin/env python
from twisted.protocols import basic
class MyChat(basic.LineReceiver):
def connectionMade(self):
print "Got new client!"
self.factory.clients.append(self)
def connectionLost(self, reason):
print "Lost a client!"
self.factory.clients.remove(self)
def lineReceived(self, line):
print "received", repr(line)
for c in self.factory.clients:
#if c != sender:
c.message(line)
def message(self, message):
if message != '':
self.transport.write(message + '\n')
from twisted.internet import protocol
from twisted.application import service, internet
factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.16.10/624 - Release Date: 1/12/2007
2:04 PM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20070112/f3f64fa8/attachment.htm
More information about the Twisted-Python
mailing list