-
Notifications
You must be signed in to change notification settings - Fork 10
/
announcer.py
50 lines (36 loc) · 1.47 KB
/
announcer.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
40
41
42
43
44
45
46
47
48
49
import logging
import socket
__all__ = ['Broadcast', 'Proxy', 'Nothing']
class Nothing(object):
def send(self, event, card, name):
logging.info("event: %s, card: %s, name: %s", event, card, name)
class Broadcast(object):
def __init__(self, port=50000):
self.port = int(port)
def send(self, event, card, name):
try:
logging.debug('Broadcasting %s to network port %d', event, self.port)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('<broadcast>', 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
event, card, name = [n.replace('\n', ' ') for n in (event, card, name)]
data = "%s\n%s\n%s" % (event, card, name)
s.sendto(data, ('<broadcast>', self.port))
except Exception, e:
logging.warn('Exception during broadcast: %s', e)
class Proxy(object):
def __init__(self, host, port=50000):
self.host = host
self.port = int(port)
def send(self, event, card, name):
try:
logging.debug('Sending %s to proxy', event)
s = socket.socket()
s.settimeout(5)
s.connect((self.host, self.port))
event, card, name = [n.replace('\n', ' ') for n in (event, card, name)]
data = "%s\n%s\n%s" % (event, card, name)
s.send(data)
s.close()
except Exception, e:
logging.warn('Exception during send: %s', e)