Skip to content

Commit 4f895c3

Browse files
committed
<fix>[testlib]: make Python SDK template compatible with Python 3
1. Replace print statements with print() function calls 2. Remove deprecated 'import sha' (hashlib.sha1 already imported) 3. Handle 'long' type removed in Python 3 with module-level try/except 4. Encode hmac/base64 arguments to bytes for Python 3 5. Fix urllib3 typo in error message Resolves: ZSTAC-83925 Change-Id: I6e746775686f6e64646b627a6f68706f77736f73
1 parent 62e8c17 commit 4f895c3

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

testlib/src/main/resources/zssdk.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
try:
55
import urllib3
66
except ImportError:
7-
print 'urlib3 is not installed, run "pip install urlib3"'
7+
print('urllib3 is not installed, run "pip install urllib3"')
88
sys.exit(1)
99

1010
import string
@@ -16,11 +16,15 @@
1616
import traceback
1717
import base64
1818
import hmac
19-
import sha
2019
from hashlib import sha1
2120
import datetime
2221
import time
2322

23+
try:
24+
int_types = (int, long)
25+
except NameError:
26+
int_types = (int,)
27+
2428
CONFIG_HOSTNAME = 'hostname'
2529
CONFIG_PORT = 'port'
2630
CONFIG_POLLING_TIMEOUT = 'default_polling_timeout'
@@ -55,7 +59,7 @@ def wrap(*args, **kwargs):
5559
try:
5660
func(*args, **kwargs)
5761
except:
58-
print traceback.format_exc()
62+
print(traceback.format_exc())
5963

6064
return wrap
6165

@@ -189,7 +193,7 @@ def _check_params(self):
189193
if value is not None and isinstance(value, str) and annotation.empty_string is False and len(value) == 0:
190194
raise SdkError('invalid parameter[%s], it cannot be an empty string' % param_name)
191195

192-
if value is not None and (isinstance(value, int) or isinstance(value, long)) \
196+
if value is not None and isinstance(value, int_types) \
193197
and annotation.number_range is not None and len(annotation.number_range) == 2:
194198
low = annotation.number_range[0]
195199
high = annotation.number_range[1]
@@ -253,10 +257,14 @@ def calculateAccessKey(self, url, date):
253257
path = elements[2].split("/", 2)
254258
path = path[2].split("?")
255259

256-
h = hmac.new(self.accessKeySecret, self.HTTP_METHOD + "\n"
257-
+ date + "\n"
258-
+ "/" + path[0], sha1)
259-
Signature = base64.b64encode(h.digest())
260+
msg = self.HTTP_METHOD + "\n" + date + "\n" + "/" + path[0]
261+
if isinstance(msg, str):
262+
msg = msg.encode('utf-8')
263+
secret = self.accessKeySecret
264+
if isinstance(secret, str):
265+
secret = secret.encode('utf-8')
266+
h = hmac.new(secret, msg, sha1)
267+
Signature = base64.b64encode(h.digest()).decode('utf-8')
260268
return "ZStack %s:%s" % (self.accessKeyId, Signature)
261269

262270
def call(self, cb=None):
@@ -482,13 +490,13 @@ def _json_http(
482490
if body is not None and not isinstance(body, str):
483491
body = json.dumps(body).encode('utf-8')
484492

485-
print '[Request]: %s url=%s, headers=%s, body=%s' % (method, uri, headers, body)
493+
print('[Request]: %s url=%s, headers=%s, body=%s' % (method, uri, headers, body))
486494
if body:
487495
headers['Content-Length'] = len(body)
488496
rsp = pool.request(method, uri, body=body, headers=headers)
489497
else:
490498
rsp = pool.request(method, uri, headers=headers)
491499

492-
print '[Response to %s %s]: status: %s, body: %s' % (method, uri, rsp.status, rsp.data)
500+
print('[Response to %s %s]: status: %s, body: %s' % (method, uri, rsp.status, rsp.data))
493501
return rsp
494502

0 commit comments

Comments
 (0)