-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·62 lines (54 loc) · 1.96 KB
/
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
#!/usr/bin/python3
import sys, os, datetime, http.server, socketserver, socket, signal
camNumberToAnalogInput = {7:7, 8:8, 9:6, 10:5}
def out(str):
print(datetime.datetime.now().strftime('[%Y-%m-%d_%H:%M:%S.%f]'), str)
class MyWebServer(socketserver.ForkingMixIn, http.server.HTTPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
class WebHandler(http.server.BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
def do_GET(self):
out('['+self.path+'] ['+repr(self.client_address)+'] do_GET')
if self.client_address[0] not in('127.0.0.1', '::1'):
self.send_response(403)
self.end_headers()
out('['+self.path+'] ['+repr(self.client_address)+'] wrong IP')
elif not self.path.startswith('/cam'):
self.send_response(404)
self.end_headers()
out('['+self.path+'] ['+repr(self.client_address)+'] not a cam')
else:
self.send_response(200)
port = 9000 + camNumberToAnalogInput[int(self.path[4:])]
self.send_header('Content-type', 'multipart/x-mixed-replace; boundary=--RevSpaceCam')
self.end_headers()
client = socket.create_connection(('localhost', port))
out('['+self.path+'] ['+repr(self.client_address)+'] open (port '+str(port)+')')
try:
while not self.wfile.closed:
data = client.recv(4096)
if not data:
out('['+self.path+'] ['+repr(self.client_address)+'] break')
break
self.wfile.write(data)
except Exception as e:
out('['+self.path+'] ['+repr(self.client_address)+'] Exception: '+repr(e))
out('['+self.path+'] ['+repr(self.client_address)+'] close')
client.close()
self.finish()
self.connection.close()
def signal_handler(signal, frame):
out('SIGINT received, shutting down')
sys.exit(0)
if __name__ == "__main__":
out('begin')
server = MyWebServer(('', 9080), WebHandler)
out('started httpserver')
try:
server.serve_forever()
except KeyboardInterrupt:
pass
server.server_close()
out('end')