Repository navigation
Expand file tree
/
Copy pathsocketListener.py
More file actions
71 lines (46 loc) · 1.94 KB
/
Copy pathsocketListener.py
File metadata and controls
71 lines (46 loc) · 1.94 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
import logger
log = logger.getLogger()
import threading
import socket
class DashListener:
def __init__(self, port, callback, hostname, validSeq =['add', 'old'], interface = ""):
self.port = port
self.interface = interface
self.callback = callback
self.spawnListener()
self.hostname = hostname
self.validSeq = validSeq
def spawnListener(self):
self.dashSockThread = threading.Thread(target = self.socketListener, name = "dashSocketThread")
self.dashSockThread.start()
def socketListener(self):
try:
self.socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
self.socket.bind(('',self.port))
while(threading.get_ident() == self.dashSockThread.ident):
msg = self.socket.recvfrom(1024)
message = str(msg[0], 'ascii')
#print(dir(message))
source = str(msg[1])
log.info("Recieved '%s' from %s",message,source)
#print(msg)
if(self.hostname in message):
#print("host found")
for name in self.validSeq:
#print("checking %s"%(name))
#goldfish has "old" so only look at first few chars
if name in message[:10]:
#print("Found Name in message")
self.callback(message, source)
break
else:
log.info("host not found %s",self.hostname)
except Exception as err:
log.exception("Socket error")
self.socket.close()
return
def sampleCallback(msg, source):
log.info("callback msg %s"%(msg))
if __name__ == '__main__':
dashButton = DashListener(2000,sampleCallback)