On Wed, Aug 18, 2021 at 3:04 AM Robin Becker <ro...@reportlab.com> wrote: > > While porting an ap from python2.7 to python3 I see this > > base64string = base64.b64encode('%s:%s' % (wsemail, wspassword)) > request.add_header("Authorization", "Basic %s" % base64string) > > in python3.x I find this works > > base64string = base64.b64encode(('%s:%s' % (wsemail, > wspassword)).encode('ascii')).decode('ascii') > request.add_header("Authorization", "Basic %s" % base64string) > > but I find the conversion to and from ascii irksome. Is there a more direct > way to create the basic auth value? > > As an additional issue I find I have no clear idea what encoding is allowed > for the components of a basic auth input.
Hmm, I'm not sure what type your wsemail and wspassword are, but one option would be to use bytes everywhere (assuming your text is all ASCII). wsemail = b"robin@becker.example" wspassword = b"correct-battery-horse-staple" base64string = base64.b64encode(b'%s:%s' % (wsemail, wspassword)) But otherwise, it's probably safest to keep using the encode and decode. As to the appropriate encoding, that's unclear according to the standard, but UTF-8 is probably acceptable. ASCII is also safe, as it'll safely error out if it would be ambiguous. https://stackoverflow.com/questions/7242316/what-encoding-should-i-use-for-http-basic-authentication ChrisA -- https://mail.python.org/mailman/listinfo/python-list