#!/usr/bin/env python
#-*-coding:utf8-*-
”’
md5sum in Python
Author: Seven
Date: 2010/09/23
Support: seven.ling7@hotmail.com
”’
try:
from hashlib import md5 #Python2.6 or later version
except:
from md5 import md5 #Python2.4 or before version
import sys
def sumfile(fobj):
m = md5()
while True:
d = fobj.read(10240)
if not d:
break
m.update(d)
del(d)
return m.hexdigest()
def sumstr(str):
m = md5()
m.update(str)
return m.hexdigest()
def md5sum(fname):
try:
f = open(fname, ‘rb’)
except:
return False
ret = sumfile(f)
f.close()
return ret
if __name__ == ‘__main__’:
for fname in sys.argv[1:]:
res=md5sum(fname)
if res == False:
res=sumstr(fname)
print ‘%32s %s’ % (res, fname)
- ”’
- md5sum in Python
- ex:
- import hashlib
- m = hashlib.md5(‘stunnel-4.48.tar.gz’)
- m.digest()
- print m.hexdigest()
- ”’
- try:
- from hashlib import md5 #Python2.5 or later version
- except:
- from md5 import md5 #Python2.4 or before version
- import sys
- import urllib
- def sumfile(fobj):
- m = md5()
- while True:
- d = fobj.read()
- if not d:
- break
- m.update(d)
- del(d)
- return m.hexdigest()
- def sumstr(str):
- m = md5()
- m.update(str)
- return m.hexdigest()
- def md5sum(fname):
- try:
- f = urllib.urlopen(fname, ‘rb’)
- #f = open(fname, ‘rb’)
- except:
- return False
- ret = sumfile(f)
- f.close()
- return ret
- res=md5sum(‘http://stunnel.cybermirror.org/stunnel-4.48.tar.gz’)
- print res