Rewrite get_snap to use requests lib as the old method did not work.
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
import io
|
import requests
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
from urllib import request
|
from urllib import parse
|
||||||
|
from io import BytesIO
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from RtspClient import RtspClient
|
from RtspClient import RtspClient
|
||||||
from resthandle import Request
|
|
||||||
|
|
||||||
|
|
||||||
class RecordingAPIMixin:
|
class RecordingAPIMixin:
|
||||||
@@ -49,19 +47,19 @@ class RecordingAPIMixin:
|
|||||||
:param timeout: Request timeout to camera in seconds
|
:param timeout: Request timeout to camera in seconds
|
||||||
:return: Image or None
|
:return: Image or None
|
||||||
"""
|
"""
|
||||||
randomstr = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
|
data = {}
|
||||||
snap = self.url + "?cmd=Snap&channel=0&rs=" \
|
data['cmd'] = 'Snap'
|
||||||
+ randomstr \
|
data['channel'] = 0
|
||||||
+ "&user=" + self.username \
|
data['rs'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
|
||||||
+ "&password=" + self.password
|
data['user'] = self.username
|
||||||
|
data['password'] = self.password
|
||||||
|
parms = parse.urlencode(data).encode("utf-8")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
req = request.Request(snap)
|
response = requests.get(self.url, params=parms, timeout=timeout)
|
||||||
req.set_proxy(Request.proxies, 'http')
|
if response.status_code == 200:
|
||||||
reader = request.urlopen(req, timeout)
|
return Image.open(BytesIO(response.content))
|
||||||
if reader.status == 200:
|
print("Could not retrieve data from camera successfully. Status:", response.stats_code)
|
||||||
b = bytearray(reader.read())
|
|
||||||
return Image.open(io.BytesIO(b))
|
|
||||||
print("Could not retrieve data from camera successfully. Status:", reader.status)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user