-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeServer.py
57 lines (44 loc) · 1.64 KB
/
homeServer.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
50
51
52
53
54
55
56
57
import json
import socket
import threading
import SocketServer
from server import *
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
class ThreadedDeviceRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
dev_data = self.request.recv(1024)
data = json.loads(dev_data);
print self.request.getpeername()
if data['type'] == 'D_REG':
response = register_handle(self.request.getpeername()[0], data['name'])
elif data['type'] == 'D_NOTIFY':
response = notify_handle(data)
elif data['type'] == 'D_DELETE':
response = delete_handle(data)
elif data['type'] == 'D_COMMAND':
response = command_handle(data)
else:
response = create_status('FAIL', 'type "' + data['type'] + '" was not found')
message = json.dumps(response)
print '%s : %s' % ('Response is ', message)
self.request.send(message)
return
class ThreadedDeviceServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__ == '__main__':
ip = get_ip_address()
port = 8080
print 'IPADDR:' + ip + ' on Port:' + str(port)
address = (ip, port) # let the kernel give us a port
server = ThreadedDeviceServer(address, ThreadedDeviceRequestHandler)
server.allow_reuse_address = True
t = threading.Thread(target=server.serve_forever)
t.setDaemon(True) # don't hang on exit
t.start()
print 'Server loop running in thread:', t.getName()
# Don't exit
while True:
pass