我正在使用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'},是否有任何方法可以将上述unicode字符串编码为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