Skip to content

Instantly share code, notes, and snippets.

@miettal
Forked from russss/grab_mjpeg_frame.py
Last active January 30, 2016 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miettal/6844563 to your computer and use it in GitHub Desktop.
Save miettal/6844563 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import socket
import sys
import base64
if len(sys.argv) != 7:
print "Usage: %s host port request user password destfile.jpg" % sys.argv[0]
sys.exit(1)
host = sys.argv[1]
port = sys.argv[2]
request = sys.argv[3]
user = sys.argv[4]
password = sys.argv[5]
filename = ''.join(sys.argv[6].split('.')[:-1])
filename_ext = sys.argv[6].split('.')[-1]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(port)))
fh = s.makefile()
# Request
fh.write(('GET ' + request + ' HTTP/1.1\r\n'
+'Host: ' + host+':'+port + '\r\n'
+'Connection: keep-alive\r\n'
+'Authorization: Basic ' + base64.b64encode(user+':'+password) + '\r\n\r\n'
))
fh.flush()
# Response
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].strip('"')
line = fh.readline()
if not boundary:
raise Exception("Can't find content-type")
n = 0
while True :
# 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()
# Write image
image = fh.read(length)
output_filename = "{0}{1:020d}.{2}".format(filename, n, filename_ext)
print output_filename
with open(output_filename, 'w') as out_fh:
out_fh.write(image)
n += 1
s.close()
@goingmywaynet
Copy link

これを参考に、Qwatch の後継品 TS-WLC2 (digest認証) 版を書きました。
https://gist.github.com/goingmywaynet/e5caea360a1f87cd0fe1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment