Skip to content

Instantly share code, notes, and snippets.

@ducin
Last active February 18, 2022 10:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/6967092 to your computer and use it in GitHub Desktop.
Save ducin/6967092 to your computer and use it in GitHub Desktop.
python twisted chat server on top of publish/subscribe server
#!/usr/bin/python
import sys, errno
if len(sys.argv) != 2:
print 'Required parameter not passed. Run:\n\n\t./chat-bomb.py <port>\n'
sys.exit(errno.EINVAL)
port = int(sys.argv[1])
# simple single-room chat server, derived from publish/subscribe
from twisted.internet import reactor, protocol
from twisted.protocols import basic
class ChatProtocol(basic.LineReceiver):
def __init__(self, factory):
self.factory = factory
self.login = None
def connectionMade(self):
self.transport.write('Register your login > ')
def connectionLost(self, reason):
self.factory.clients.pop(self.login)
for login, protocol in self.factory.clients.items():
protocol.sendLine("%s has quit the chat room" % (self.login,))
def lineReceived(self, line):
if len(line) == 0:
return
if not self.login:
self.login = line
self.factory.clients[self.login] = self
for login, protocol in self.factory.clients.items():
protocol.sendLine("%s has joined the chat room" % (self.login,))
elif line == 'exit':
self.transport.write('Bye!\n')
self.transport.loseConnection()
elif line == 'bomb':
self.factory.loseAllConnections()
elif line == 'exit-bomb':
reactor.callLater(3, self.factory.loseAllConnections)
for login, protocol in self.factory.clients.items():
protocol.sendLine("%s is running away..." % (self.login,))
self.transport.loseConnection()
else:
for login, protocol in self.factory.clients.items():
if login != self.login:
protocol.sendLine("<%s> %s" % (self.login, line))
class ChatFactory(protocol.Factory):
def __init__(self):
self.clients = {}
def buildProtocol(self, addr):
return ChatProtocol(self)
def loseAllConnections(self):
for protocol in self.clients.values():
protocol.sendLine('kaboom!')
protocol.transport.loseConnection()
reactor.listenTCP(port, ChatFactory())
reactor.run()
#!/usr/bin/python
import sys, errno
if len(sys.argv) != 2:
print 'Required parameter not passed. Run:\n\n\t./chat-server.py <port>\n'
sys.exit(errno.EINVAL)
port = int(sys.argv[1])
# simple single-room chat server, derived from publish/subscribe
from twisted.internet import reactor, protocol
from twisted.protocols import basic
class ChatProtocol(basic.LineReceiver):
def __init__(self, factory):
self.factory = factory
self.login = None
def connectionMade(self):
self.transport.write('Register your login > ')
def connectionLost(self, reason):
self.factory.clients.pop(self.login)
for login, protocol in self.factory.clients.items():
protocol.sendLine("%s has quit the chat room" % (self.login,))
def lineReceived(self, line):
if len(line) == 0:
return
if not self.login:
self.login = line
self.factory.clients[self.login] = self
for login, protocol in self.factory.clients.items():
protocol.sendLine("%s has joined the chat room" % (self.login,))
elif line == 'exit':
self.transport.write('Bye!\n')
self.transport.loseConnection()
else:
for login, protocol in self.factory.clients.items():
if login != self.login:
protocol.sendLine("<%s> %s" % (self.login, line))
class ChatFactory(protocol.Factory):
def __init__(self):
self.clients = {}
def buildProtocol(self, addr):
return ChatProtocol(self)
reactor.listenTCP(port, ChatFactory())
reactor.run()
#!/usr/bin/python
import sys, errno
if len(sys.argv) != 2:
print 'Required parameter not passed. Run:\n\n\t./publish-subscribe.py <port>\n'
sys.exit(errno.EINVAL)
port = int(sys.argv[1])
# almost original publish/subscribe snippet from twisted homepage
from twisted.internet import reactor, protocol
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)
reactor.listenTCP(port, PubFactory())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment