Created
August 29, 2017 19:04
-
-
Save maethu/159227cda2ff0e146ae8e708e8d9a6a5 to your computer and use it in GitHub Desktop.
This file contains 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
JPEG_START_BYTE = '\xff\xd8' | |
JPEG_END_BYTE = '\xff\xd9' | |
def get_jpeg(stream): | |
start_pos = None | |
end_pos = None | |
while True: | |
frame_bytes = stream.read(1024) | |
start_pos = None | |
end_pos = None | |
if JPEG_START_BYTE in frame_bytes: | |
start_pos = frame_bytes.index(JPEG_START_BYTE) | |
if JPEG_END_BYTE in frame_bytes: | |
end_pos = frame_bytes.index(JPEG_END_BYTE) | |
yield frame_bytes[start_pos:end_pos] | |
if start_pos and end_pos: | |
break | |
site = opener.open(url) | |
data = ''.join(tuple(get_jpeg(site))) # or next(get_jpeg(site)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment