-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrab_mjpeg_frame.py
executable file
·45 lines (35 loc) · 1.09 KB
/
grab_mjpeg_frame.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
#!/usr/bin/env python
import socket
import sys
if len(sys.argv) != 3:
print "Usage: %s host:port destfile.jpg" % sys.argv[0]
sys.exit(1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host, port = sys.argv[1].split(':')
s.connect((host, int(port)))
fh = s.makefile()
# Read in HTTP headers:
line = fh.readline()
while line.strip() != '':
parts = line.split(':')
if len(parts) > 1 and parts[0].lower() == 'content-type':
# Extract boundary string from content-type
content_type = parts[1].strip()
boundary = content_type.split(';')[1].split('=')[1]
line = fh.readline()
if not boundary:
raise Exception("Can't find content-type")
# Seek ahead to the first chunk
while line.strip() != boundary:
line = fh.readline()
# Read in chunk headers
while line.strip() != '':
parts = line.split(':')
if len(parts) > 1 and parts[0].lower() == 'content-length':
# Grab chunk length
length = int(parts[1].strip())
line = fh.readline()
image = fh.read(length)
with open(sys.argv[2], 'w') as out_fh:
out_fh.write(image)
s.close()