DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5
原来的python脚本是在Ubuntu 14.04 64bit上写的,运行没有问题,但是在CentOS 6.3上的crontab中定时执行时,每次都报
DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5
脚本如下:
#/usr/bin/env python
#coding:utf-8
import md5
hash = md5.new()
hash.update('spam,spam,and egges')
print repr(hash.digest())
解决方法:
与系统默认安装的python版本有关,CentOS 6.3默认的版本是2.6,Ubuntu 14.04 64bit上默认的版本是2.7,改成下面的脚本就可以都兼容了。
#!/usr/bin/env python
#encoding: utf-8
import types
#get md5 of a specified string
def get_md5(s):
if type(s) is types.StringType:
try:
import hashlib
m = hashlib.md5()
except ImportError:
# for python < 2.5
import md5
m = md5.new()
m.update(s)
return repr(m.hexdigest())
else:
return ''
if __name__ == '__main__':
print get_md5("tao_627@aliyun.com")
在终端运行的结果是
还没有评论,来说两句吧...