-
Notifications
You must be signed in to change notification settings - Fork 5
/
a9.py
64 lines (57 loc) · 2.03 KB
/
a9.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
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from socket import *
import struct
def initiate_camera(sock):
size = sock.sendto(struct.pack('>BB', 0x30, 0x67), ('192.168.4.153', 8070))
size += sock.sendto(struct.pack('>BB', 0x30, 0x66), ('192.168.4.153', 8070))
size += sock.sendto(struct.pack('>BB', 0x42, 0x76), ('192.168.4.153', 8080))
return (size == 6)
def packet_is_image_start(buf):
for i in range(0, len(buf)-1):
if(buf[i] == 0xff and buf[i+1] == 0xd8):
return True
return False
def packet_is_image_end(buf):
for i in range(0, len(buf)-1):
if(buf[i] == 0xff and buf[i+1] == 0xd9):
return True
return False
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('.mjpg'):
self.send_response(200)
self.send_header('Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
frame = bytearray()
while camera_initialized:
(buf, rinfo) = s.recvfrom(4096)
port = rinfo[1]
if(port == 8080):
if(packet_is_image_start(buf)):
frame = bytearray(buf)[8:]
else:
frame += bytearray(buf)[8:]
if(packet_is_image_end(buf)):
self.send_header('Content-type', 'image/jpeg')
self.send_header('Content-length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b"\r\n--jpgboundary\r\n")
return
if self.path.endswith('.html'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'<html><head></head><body><img src="http://127.0.0.1:8081/cam.mjpg"/></body></html>')
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
if __name__ == '__main__':
s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind(('',0))
camera_initialized = initiate_camera(s)
server = ThreadedHTTPServer(('localhost', 8081), CamHandler)
print("server started at http://127.0.0.1:8081/cam.html")
server.serve_forever()