From: Cyril Roelandt Date: Tue, 28 Jul 2015 14:43:00 +0000 (+0200) Subject: Python 3: pass bytes to base64.encode{string,bytes} X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=74cdfb1bab12bf54bbc64267c5fb92cc35d6b40b;p=openstack-build%2Fneutron-build.git Python 3: pass bytes to base64.encode{string,bytes} In Python 3, base64.encodestring expects bytes. Also, base64.encodebytes should be used in order not to trigger a DeprecationWarning. Change-Id: Iaae377b612feda7e01963f0d527c898bc0fd21d1 Blueprint: neutron-python3 --- diff --git a/neutron/plugins/cisco/n1kv/n1kv_client.py b/neutron/plugins/cisco/n1kv/n1kv_client.py index f7a54f50f..edbf5e7db 100644 --- a/neutron/plugins/cisco/n1kv/n1kv_client.py +++ b/neutron/plugins/cisco/n1kv/n1kv_client.py @@ -19,6 +19,7 @@ import netaddr from oslo_log import log as logging from oslo_serialization import jsonutils import requests +import six from neutron.common import exceptions as n_exc from neutron.extensions import providernet @@ -32,6 +33,22 @@ from neutron.plugins.cisco.extensions import n1kv LOG = logging.getLogger(__name__) +def safe_b64_encode(s): + if six.PY3: + method = base64.encodebytes + else: + method = base64.encodestring + + if isinstance(s, six.text_type): + s = s.encode('utf-8') + encoded_string = method(s).rstrip() + + if six.PY3: + return encoded_string.decode('utf-8') + else: + return encoded_string + + class Client(object): """ @@ -502,7 +519,7 @@ class Client(object): """ username = c_cred.Store.get_username(host_ip) password = c_cred.Store.get_password(host_ip) - auth = base64.encodestring("%s:%s" % (username, password)).rstrip() + auth = safe_b64_encode("%s:%s" % (username, password)) header = {"Authorization": "Basic %s" % auth} return header