From 693c12d40bb51eb3a8029aba1a9c7248c081a44c Mon Sep 17 00:00:00 2001 From: C1-BA-B1-F3 Date: Sat, 3 Oct 2026 16:22:44 +0800 Subject: [PATCH] fix(http): use b64encode with explicit encode/decode for Python 3 base64.encodestring was removed in Python 3.9 and expected a str in Python 3 (raising TypeError: expected bytes-like object, not str). Use base64.b64encode with explicit UTF-8 encode/decode, which works on all Python 3 versions. Fixes #130 --- stetl/inputs/httpinput.py | 3 ++- stetl/outputs/httpoutput.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/stetl/inputs/httpinput.py b/stetl/inputs/httpinput.py index 90998af..b15be9e 100644 --- a/stetl/inputs/httpinput.py +++ b/stetl/inputs/httpinput.py @@ -113,7 +113,8 @@ def add_authorization(self, request): # Basic auth: http://mozgovipc.blogspot.nl/2012/06/python-http-basic-authentication-with.html # base64 encode username and password # write the Authorization header like: 'Basic base64encode(username + ':' + password) - auth_val = base64.encodestring('%s:%s' % (auth_creds['user'], auth_creds['password'])) + creds = '%s:%s' % (auth_creds['user'], auth_creds['password']) + auth_val = base64.b64encode(creds.encode('utf-8')).decode('utf-8') auth_val = "Basic %s" % auth_val elif auth_type == 'token': # Bearer Type, see eg. https://tools.ietf.org/html/rfc6750 diff --git a/stetl/outputs/httpoutput.py b/stetl/outputs/httpoutput.py index 03fafc4..ab8370f 100644 --- a/stetl/outputs/httpoutput.py +++ b/stetl/outputs/httpoutput.py @@ -107,8 +107,9 @@ def post(self, packet, payload): # basic auth: http://mozgovipc.blogspot.nl/2012/06/python-http-basic-authentication-with.html # base64 encode the username and password # write the Authorization header like: 'Basic base64encode(username + ':' + password) - if self.user is not None: - auth = base64.encodestring('%s:%s' % (self.user, self.password)).replace('\n', '') + if self.user is not None and self.password is not None: + creds = '%s:%s' % (self.user, self.password) + auth = base64.b64encode(creds.encode('utf-8')).decode('utf-8') webservice.putheader("Authorization", "Basic %s" % auth) webservice.endheaders()