|
21 | 21 | import socket
|
22 | 22 |
|
23 | 23 | from PIL import Image # , UnidentifiedImageError
|
24 |
| -import requests |
25 | 24 | import rpyc
|
26 | 25 |
|
27 | 26 | try:
|
@@ -194,67 +193,68 @@ def iter_content(self):
|
194 | 193 | yield self.read_mjpeg_frame(self.stream, self.boundary)
|
195 | 194 |
|
196 | 195 |
|
197 |
| -class MjpgReader(): |
198 |
| - """ |
199 |
| - MJPEG format |
200 |
| -
|
201 |
| - Content-Type: multipart/x-mixed-replace; boundary=--BoundaryString |
202 |
| - --BoundaryString |
203 |
| - Content-type: image/jpg |
204 |
| - Content-Length: 12390 |
205 |
| -
|
206 |
| - ... image-data here ... |
207 |
| -
|
208 |
| -
|
209 |
| - --BoundaryString |
210 |
| - Content-type: image/jpg |
211 |
| - Content-Length: 12390 |
212 |
| -
|
213 |
| - ... image-data here ... |
214 |
| - """ |
215 |
| - |
216 |
| - def __init__(self, url: str): |
217 |
| - self._url = url |
218 |
| - self.session = requests.Session() |
219 |
| - |
220 |
| - def iter_content(self): |
221 |
| - """ |
222 |
| - Raises: |
223 |
| - RuntimeError |
224 |
| - """ |
225 |
| - |
226 |
| - r = self.session.get(self._url, stream=True, timeout=3) |
227 |
| - # r = requests.get(self._url, stream=True, timeout=3) |
228 |
| - |
229 |
| - # parse boundary |
230 |
| - content_type = r.headers['content-type'] |
231 |
| - index = content_type.rfind("boundary=") |
232 |
| - assert index != 1 |
233 |
| - boundary = content_type[index+len("boundary="):] + "\r\n" |
234 |
| - boundary = boundary.encode('utf-8') |
235 |
| - |
236 |
| - rd = io.BufferedReader(r.raw) |
237 |
| - while True: |
238 |
| - self._skip_to_boundary(rd, boundary) |
239 |
| - length = self._parse_length(rd) |
240 |
| - yield rd.read(length) |
241 |
| - |
242 |
| - def _parse_length(self, rd) -> int: |
243 |
| - length = 0 |
244 |
| - while True: |
245 |
| - line = rd.readline() |
246 |
| - if line == b'\r\n': |
247 |
| - return length |
248 |
| - if line.startswith(b"Content-Length"): |
249 |
| - length = int(line.decode('utf-8').split(": ")[1]) |
250 |
| - assert length > 0 |
251 |
| - |
252 |
| - def _skip_to_boundary(self, rd, boundary: bytes): |
253 |
| - for _ in range(10): |
254 |
| - if boundary in rd.readline(): |
255 |
| - break |
256 |
| - else: |
257 |
| - raise RuntimeError("Boundary not detected:", boundary) |
| 196 | +# import requests |
| 197 | +# class MjpgReader(): |
| 198 | +# """ |
| 199 | +# MJPEG format |
| 200 | + |
| 201 | +# Content-Type: multipart/x-mixed-replace; boundary=--BoundaryString |
| 202 | +# --BoundaryString |
| 203 | +# Content-type: image/jpg |
| 204 | +# Content-Length: 12390 |
| 205 | + |
| 206 | +# ... image-data here ... |
| 207 | + |
| 208 | + |
| 209 | +# --BoundaryString |
| 210 | +# Content-type: image/jpg |
| 211 | +# Content-Length: 12390 |
| 212 | + |
| 213 | +# ... image-data here ... |
| 214 | +# """ |
| 215 | + |
| 216 | +# def __init__(self, url: str): |
| 217 | +# self._url = url |
| 218 | +# self.session = requests.Session() |
| 219 | + |
| 220 | +# def iter_content(self): |
| 221 | +# """ |
| 222 | +# Raises: |
| 223 | +# RuntimeError |
| 224 | +# """ |
| 225 | + |
| 226 | +# r = self.session.get(self._url, stream=True, timeout=3) |
| 227 | +# # r = requests.get(self._url, stream=True, timeout=3) |
| 228 | + |
| 229 | +# # parse boundary |
| 230 | +# content_type = r.headers['content-type'] |
| 231 | +# index = content_type.rfind("boundary=") |
| 232 | +# assert index != 1 |
| 233 | +# boundary = content_type[index+len("boundary="):] + "\r\n" |
| 234 | +# boundary = boundary.encode('utf-8') |
| 235 | + |
| 236 | +# rd = io.BufferedReader(r.raw) |
| 237 | +# while True: |
| 238 | +# self._skip_to_boundary(rd, boundary) |
| 239 | +# length = self._parse_length(rd) |
| 240 | +# yield rd.read(length) |
| 241 | + |
| 242 | +# def _parse_length(self, rd) -> int: |
| 243 | +# length = 0 |
| 244 | +# while True: |
| 245 | +# line = rd.readline() |
| 246 | +# if line == b'\r\n': |
| 247 | +# return length |
| 248 | +# if line.startswith(b"Content-Length"): |
| 249 | +# length = int(line.decode('utf-8').split(": ")[1]) |
| 250 | +# assert length > 0 |
| 251 | + |
| 252 | +# def _skip_to_boundary(self, rd, boundary: bytes): |
| 253 | +# for _ in range(10): |
| 254 | +# if boundary in rd.readline(): |
| 255 | +# break |
| 256 | +# else: |
| 257 | +# raise RuntimeError("Boundary not detected:", boundary) |
258 | 258 |
|
259 | 259 | ########################################################################################################################################
|
260 | 260 |
|
|
0 commit comments