import md5
import types def get_md5(data):
if type(data) is not types.StringType: # 检验输入的数据是否为字符串
print 'Data must be <str> type'
return m = md5.new() # 创建md5对象
m.update(data) # 更新对象,计算data的md5值
return m.hexdigest() # 16进制返回值,32位 if __name__ == '__main__':
my_md5 = get_md5('I have a good day.')
print my_md5
用hashlib
import hashlib
import types def get_md5(data):
if type(data) is not types.StringType: # 检验输入的数据是否为字符串
print 'Data must be <str> type'
return m = hashlib.md5() # 创建md5对象
m.update(data) # 更新对象,计算data的md5值
return m.hexdigest() # 16进制返回值,32位 if __name__ == '__main__':
my_md5 = get_md5('I have a good day.')
print my_md5