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])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 概述 对象存储服务(Object Storage Service,简称OSS),OSS提供统一存储的解决方案 方便...
    yaoshiyou阅读 2,014评论 0 1
  • API定义规范 本规范设计基于如下使用场景: 请求频率不是非常高:如果产品的使用周期内请求频率非常高,建议使用双通...
    有涯逐无涯阅读 2,801评论 0 6
  • test2 test2 API列表 API名称 认证方式 描述 getUserInfo APP ge...
    tommyhxh阅读 1,520评论 0 0
  • 指令集 lua_capture_error_log lua_use_default_type lua_malloc...
    吃瓜的东阅读 12,158评论 0 2