From 8810709dc6448e2232a3f6355d8f5315f5970c64 Mon Sep 17 00:00:00 2001 From: MichaIng Date: Tue, 12 Apr 2022 15:09:35 +0200 Subject: [PATCH] Fix base64 encoding b64encode takes and returns only bytes, so the credentials need to be byte-encoded first and the returned bytes object decoded afterwards (ASCII works for base64 strings). Additionally some %-formattings have been replaced with f-strings, fix some syntax errors and satisfy pre-commit black checks. --- motioneye/uploadservices.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/motioneye/uploadservices.py b/motioneye/uploadservices.py index 576b5fc33..7689bbe0a 100644 --- a/motioneye/uploadservices.py +++ b/motioneye/uploadservices.py @@ -895,16 +895,18 @@ def __init__(self, camera_id): UploadService.__init__(self, camera_id) def _request(self, url, method, body=None): - base64string = b64encode(f'{self._username}:{self._password}') - headers = {'Authorization': 'Basic %s' % base64string} + base64string = b64encode(f'{self._username}:{self._password}'.encode()).decode( + 'ascii' + ) + headers = {'Authorization': f'Basic {base64string}'} if body is not None: - headers.update('Content-Length', '%d' % len(body)) - self.debug('request: ' + method + ' ' + url) + headers.update({'Content-Length': len(body)}) + self.debug(f'request: {method} {url}') request = urllib.request.Request(url, data=body, headers=headers) request.get_method = lambda: method try: utils.urlopen(request) - except urllib.HTTPError as e: + except urllib.error.HTTPError as e: if method == 'MKCOL' and e.code == 405: self.debug( 'MKCOL failed with code 405, this is normal if the folder exists' @@ -999,7 +1001,7 @@ def upload_data(self, filename, mime_type, data, ctime, camera_name): conn.cwd(path) self.debug(f'uploading {filename} of {len(data)} bytes') - conn.storbinary('STOR %s' % filename, io.StringIO(data)) + conn.storbinary(f'STOR {filename}', io.StringIO(data)) self.debug('upload done')