DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5

叁歲伎倆 2022-08-02 07:17 30阅读 0赞

原来的python脚本是在Ubuntu 14.04 64bit上写的,运行没有问题,但是在CentOS 6.3上的crontab中定时执行时,每次都报

DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5

脚本如下:

  1. #/usr/bin/env python
  2. #coding:utf-8
  3. import md5
  4. hash = md5.new()
  5. hash.update('spam,spam,and egges')
  6. print repr(hash.digest())

解决方法:

与系统默认安装的python版本有关,CentOS 6.3默认的版本是2.6,Ubuntu 14.04 64bit上默认的版本是2.7,改成下面的脚本就可以都兼容了。

  1. #!/usr/bin/env python
  2. #encoding: utf-8
  3. import types
  4. #get md5 of a specified string
  5. def get_md5(s):
  6. if type(s) is types.StringType:
  7. try:
  8. import hashlib
  9. m = hashlib.md5()
  10. except ImportError:
  11. # for python < 2.5
  12. import md5
  13. m = md5.new()
  14. m.update(s)
  15. return repr(m.hexdigest())
  16. else:
  17. return ''
  18. if __name__ == '__main__':
  19. print get_md5("tao_627@aliyun.com")

在终端运行的结果是

Center

发表评论

表情:
评论列表 (有 0 条评论,30人围观)

还没有评论,来说两句吧...

相关阅读