python字典操作get()

在阿里云OSS API用户身份验证的时候,需要按照提供的算法对参数进行验证

Authorization = "OSS " + AccessKeyId + ":" + Signature
Signature = base64(hmac-sha1(AccessKeySecret,
            VERB + "\n"
            + Content-MD5 + "\n" 
            + Content-Type + "\n" 
            + Date + "\n" 
            + CanonicalizedOSSHeaders
            + CanonicalizedResource))

其中Content-MD5和Content-Type可以为空,所以在获取字典headers中的值时,若Content-MD5和Content-Type不存在时,我们可以通过get()方法将其设置为空
如此,可以将其上算法代码实现为:

def getAuthString():
   headers_string = getHeadersString()
   resource_string = getResourceString()
   headers = {'Date' : formatdate(None, usegmt=True)}
   content_md5 = headers.get('content-MD5', '')
   content_type = headers.get('content-Type', '')
   date = headers.get('date', '')
   return '\n'.join([req.method,
                          content_md5,
                          content_type,
                          date,
                          headers_string + resource_string])

你可能感兴趣的:(python字典操作get())