如何让base64在python2和python3都运行通过

args = [1,2,3,4]
text = json.dumpsargs
query = base64.b64encodetext
auth = hashlib.md5query.hexdigest
printauth

这段代码在python2.7下面通过,但是python3.5会报错:
TypeError: str does not support the buffer interface

更改为这样以后,python3可以通过了

args = [1,2,3,4]
text = json.dumpsargs
query = base64.b64encodebytestext, utf-8, auth = hashlib.md5query.hexdigest
printauth

但是python2又会报错:
TypeError: str takes at most 1 argument 2 given

该怎样修改才能使代码在python2和python3都运行通过呢?

import base64, json, hashlib args = [1,2,3,4]
text = json.dumpsargs
try: text = bytestext, utf-8
except: pass
query = base64.b64encodetext
auth = hashlib.md5query.hexdigest
printauth

args = [1,2,3,4]
text = json.dumpsargs #这里添加了一个bytearray类型的变量temp
temp = bytearraytext, encoding=ascii query = base64.b64encodetemp
auth = hashlib.md5query.hexdigest
printauth

import base64, json, hashlib, sys args = [1, 2, 3, 4]
text = json.dumpsargs
if sys.version_info > 3, 0: text = bytestext, utf-8
query = base64.b64encodetext
auth = hashlib.md5query.hexdigest
printauth

发表评论

电子邮件地址不会被公开。 必填项已用*标注