全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网
视频直播

鉴权代码示例

更新时间:2017-11-07 12:05:59

概述

URL鉴权规则请查阅 直播鉴权文档,通过这个 demo 您可以根据业务需要,方便的对URL进行鉴权处理。以下Python Demo包含直播所用的A种鉴权方式。

Python版本

  1. import re
  2. import time
  3. import hashlib
  4. import datetime
  5. def md5sum(src):
  6. m = hashlib.md5()
  7. m.update(src)
  8. return m.hexdigest()
  9. def a_auth(uri, key, exp):
  10. p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
  11. if not p:
  12. return None
  13. m = p.match(uri)
  14. scheme, host, path, args = m.groups()
  15. if not scheme: scheme = "http://"
  16. if not path: path = "/"
  17. if not args: args = ""
  18. rand = "0" # "0" by default, other value is ok
  19. uid = "0" # "0" by default, other value is ok
  20. sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
  21. hashvalue = md5sum(sstring)
  22. auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
  23. if args:
  24. return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
  25. else:
  26. return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)
  27. def main():
  28. uri = "http://xc.cdnpe.com/ping?foo=bar" # original uri
  29. key = "<input private key>" # private key of authorization
  30. exp = int(time.time()) + 1 * 3600 # expiration time: 1 hour after current itme
  31. authuri = a_auth(uri, key, exp) # auth type:
  32. print("URL : %s\nAUTH: %s" %(uri, authuri))
  33. if __name__ == "__main__":
  34. main()
本文导读目录