如何让base64在python2和python3都运行通过
高洛峰
高洛峰 2016-11-09 14:10:05
0
2
783
args = [1,2,3,4] text = json.dumps(args) query = base64.b64encode(text) auth = hashlib.md5(query).hexdigest() print(auth)

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

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

args = [1,2,3,4] text = json.dumps(args) query = base64.b64encode(bytes(text, 'utf-8'), ) auth = hashlib.md5(query).hexdigest() print(auth)

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

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


高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

répondre à tous (2)
代言
args = [1,2,3,4] text = json.dumps(args) #这里添加了一个bytearray类型的变量temp temp = bytearray(text, encoding='ascii') query = base64.b64encode(temp) auth = hashlib.md5(query).hexdigest() print(auth)


    三叔
    import base64, json, hashlib args = [1,2,3,4] text = json.dumps(args) try: text = bytes(text, 'utf-8') except: pass query = base64.b64encode(text) auth = hashlib.md5(query).hexdigest() print(auth)


      Derniers téléchargements
      Plus>
      effets Web
      Code source du site Web
      Matériel du site Web
      Modèle frontal
      À propos de nous Clause de non-responsabilité Sitemap
      Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!