-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
34 lines (25 loc) · 954 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
import logging
from gevent.server import DatagramServer
class ChatServer(DatagramServer):
def __init__(self, *args):
self.clients = set()
super(ChatServer, self).__init__(*args)
def handle(self, data, address):
logging.debug('%s: received %r' % (address[0], data))
if address not in self.clients:
self.clients.add(address)
if data == 'HELO':
# Handshake
self.socket.sendto('OHAI\x00', address)
return
self.broadcast(address, data)
def broadcast(self, origin, message):
for address in self.clients:
if address == origin:
continue
logging.debug("sending %r to %r" % (message, address))
self.socket.sendto(message, address)
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
logging.info("Chat server starting")
ChatServer(':9000').serve_forever()