-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_http_server.py
100 lines (91 loc) · 2.85 KB
/
websocket_http_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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import socket
import os
import re
from websocket_server import WebsocketServer
import time
import threading
import ConfigParser
import base64
import sys
import cv2
import cv
import numpy as np
parser = ConfigParser.RawConfigParser()
configFilePath =r'server.conf'
parser.read(configFilePath)
http_interface = parser.get('Server-Configurations', 'HTTP_Interface')
http_port = parser.get('Server-Configurations', 'HTTP_Port')
http_listen = parser.get('Server-Configurations', 'HTTP_Listen')
http_listen = int(http_listen)
http_recieve = parser.get('Server-Configurations', 'HTTP_Recieve')
http_recieve = int(http_recieve)
websocket_interface = parser.get('Server-Configurations', 'Websocket_Interface')
websocket_port = parser.get('Server-Configurations', 'Websocket_Port')
websocket_port = int(websocket_port)
image_path = parser.get('Server-Configurations', 'Image')
def http_server():
addr = socket.getaddrinfo(http_interface, http_port)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(http_listen)
print('listening on', addr)
while True:
c1, addr = s.accept()
print repr(c1.recv(http_recieve))
print('client connected from',addr)
data = open("index.html", 'rb')
data = data.read()
c1.send(data)
time.sleep(.01)
c1.close()
def setIP():
while True:
ip = os.popen('ip addr show eth0 | grep "\<inet\>" | awk \'{ print $2 }\' | awk -F "/" \'{ print $1 }\'').read().strip()
print(ip)
html_code = []
html = open("index.html","r")
for line in html:
html_code.append(line)
for line in html_code:
match = re.findall('var s = new WebSocket',line)
if len(match)>0:
position=html_code.index(line)
html_code[9] = ' var s = new WebSocket("ws://%s:9876/");\n' %ip
with open("index.html","w") as index:
for line in html_code:
index.write(str(line))
time.sleep(60)
def websocket_server():
global i
i=0
def new_client(client, server):
global i
i=((i+1))
def start_data_stream():
while True:
try:
with open(image_path, "rb") as image_file:
thermal=base64.b64encode(image_file.read())
server.send_message_to_all(thermal)
time.sleep(.02)
except:
with open(image_path,"rb") as image_file:
thermal=base64.b64encode(image_file.read())
server.send_message_to_all(thermal)
time.sleep(.02)
if i == 1:
start_data_stream()
else:
pass
print("New client connected and was given id %d" % client['id'])
def client_left(client, server):
print("Client(%d) disconnected" % client['id'])
PORT=9876
server = WebsocketServer(websocket_port,host=websocket_interface)
server.set_fn_new_client(new_client)
server.set_fn_client_left(client_left)
server.run_forever()
threading.Thread(target=setIP).start()
threading.Thread(target=websocket_server).start()
threading.Thread(target=http_server).start()