【发布时间】:2015-10-17 23:08:03
【问题描述】:
我正在使用 Flask-Python 创建一个 REST API。其中一个 url (/uploads) 接收(一个 POST HTTP 请求)和一个 JSON '{"src":"void", "settings":"my settings"}'。我可以单独提取每个对象并编码为一个字节字符串,然后可以在 python 中使用 hashlib 对其进行散列。但是,我的目标是获取整个字符串,然后进行编码,使其看起来像...myfile.encode('utf-8')。打印 myfile 显示如下 >> {u'src':u'void', u'settings':u'my settings'},无论如何我可以将上面的 unicoded 字符串编码为 utf-8 到一个序列hashlib.sha1(mayflies.encode('uff-8') 的字节。请让我知道以获得更多说明。提前致谢。
fileSRC = request.json['src']
fileSettings = request.json['settings']
myfile = request.json
print myfile
#hash the filename using sha1 from hashlib library
guid_object = hashlib.sha1(fileSRC.encode('utf-8')) // this works however I want myfile to be encoded not fileSRC
guid = guid_object.hexdigest() //this works
print guid
【问题讨论】:
-
澄清:您是否要使 json 成为字符串并对其进行哈希处理?
-
您好,感谢您的回复。我从你的问题中得到了答案,它现在有效。非常感谢。
-
我使用了 ...jsonContent = json.dumps(request.json)..then guid_object = hashlib.sha1(jsonContent.encode('utf-8'))。现在可以了。
标签: python json python-2.7 unicode utf-8