-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
39 lines (27 loc) · 953 Bytes
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
from twisted.internet import protocol, reactor
colors = ['7f8c8d', 'c0392b', '2c3e50', '8e44ad', '27ae60']
transports = set()
def esc_markup(msg):
return (msg.replace('&', '&')
.replace('[', '&bl;')
.replace(']', '&br;'))
class Chat(protocol.Protocol):
def connectionMade(self):
self.color = colors.pop()
colors.insert(0, self.color)
def dataReceived(self, data):
transports.add(self.transport)
if ':' not in data:
return
user, msg = data.split(':', 1)
for t in transports:
if t is not self.transport:
t.write('[b][color={}]{}:[/color][/b] {}'
.format(self.color, user,
esc_markup(msg)))
class ChatFactory(protocol.Factory):
def buildProtocol(self, addr):
return Chat()
reactor.listenTCP(9096, ChatFactory())
reactor.run()