This post has already been read 2032 times!
What is Twisted?
Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license. Twisted runs on Python 2 and an ever growing subset also works with Python 3.
Here's a simple publish/subscribe server, where clients see all messages posted by other clients:
from twisted.internet import reactor, protocol, endpoints
from twisted.protocols import basic
class PubProtocol(basic.LineReceiver):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
self.factory.clients.add(self)
def connectionLost(self, reason):
self.factory.clients.remove(self)
def lineReceived(self, line):
for c in self.factory.clients:
c.sendLine("<{}> {}".format(self.transport.getHost(), line))
class PubFactory(protocol.Factory):
def __init__(self):
self.clients = set()
def buildProtocol(self, addr):
return PubProtocol(self)
endpoints.serverFromString(reactor, "tcp:1025").listen(PubFactory())
reactor.run()
You can test this out by opening two terminals and doing telnet localhost 1025 in each, then typing things.
source : https://twistedmatrix.com/trac/