Last active
February 8, 2025 20:18
-
-
Save knzm/43520301662c8df13e1bdefb750a0040 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import closing | |
from http.client import HTTPConnection, parse_headers | |
def get_mjpeg_frame(host, path, method='GET', connection_cls=HTTPConnection): | |
with closing(connection_cls(host)) as conn: | |
conn.request(method, path, headers={'Host': host}) | |
with closing(conn.getresponse()) as res: | |
assert res.status == 200 | |
assert res.headers.get_content_type() == 'multipart/x-mixed-replace' | |
boundary = res.headers.get_param('boundary', header='Content-Type') | |
assert boundary is not None | |
if not boundary.startswith("--"): | |
boundary = "--" + boundary | |
line = res.fp.readline().decode('utf-8').strip() | |
assert line == boundary | |
part_headers = parse_headers(res.fp) | |
assert part_headers.get_content_type() == 'image/jpeg' | |
length = int(part_headers.get('Content-Length')) | |
frame = res.read(length) | |
return frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment