Linux to verify the file MD5 value, the easiest way is to execute the md5sum command
md5sum filename
Originally intended to use subprocess to invoke system commands to get MD5 values,
Import Subprocess,shlex
cmd = "md5sum filename"
p = subprocess (Shlex.split (cmd), stdout=subprocess. PIPE)
print p.stdout.read ()
But Python has its own MD5 module Hashlib, which is much simpler to use,
Instructions for using the Python hashlib module http://docs.python.org/2/library/hashlib.html
FD = HASHLIB.MD5 () #获取一个MD5加密算法对象
Fd.update ("string") #指定需要加密的字符串
Fd.hexdigest () #获取加密后的16进制字符串
Instance
#!/usr/bin/env python
#coding: Utf-8 3 4 import sys
import hashlib
def md5sum (filename):
fd = open ( FileName, "R")
Fcont = FD.R
fd.close ()
fmd5 = HASHLIB.MD5 (fcont) return
fmd5
If __name__ = "__ main__ ":
fmd5 = md5sum (sys.argv[1])
print fmd5.hexdigest ()
where fmd5 = Hashlib.md5 (Fcont) is equivalent to
FMD5 = HASHLIB.MD5 (Fcont)
Fmd5.update (Fcont)
It should be noted that the incoming HASHLIB.MD5 () should be the contents of the file rather than the filename, so that the contents of the file generated MD5 check code;
In addition, the call to Hashlib.md5 () after the return of an object, want to get the same effect under Linux md5sum, but also call the Hexdigest () method.
However, this method is a bit too rough, when the test of large files, all the contents of the file read into memory, it is very expensive,
The example http://blog.csdn.net/shanliangliuxing/article/details/10115397 is given on the internet,
According to the file block length, get the contents of the file sequentially read into memory, update the checksum through update (),
#!/usr/bin/env python 2 #coding: Utf-8 3 import hashlib def md5hex (word): "" "MD5 encryption algorithm, return 32 bit lowercase 16 symbol" " If Isinstance (Word, Unicode): Word = Word.encode ("Utf-8") Elif not isinstance (Word, str): WOR d = str (word) m = hashlib.md5 () m.update (word) return m.hexdigest () def md5sum (fname): "" "The MD of the computed file
5 Value "" "Def Read_chunks (FH): Fh.seek (0) chunk = fh.read (8096) while chunk: Yield chunk chunk = fh.read (8096) Else: #最后要将游标放回文件开头 fh.seek (0) m = hashlib.md5
() if Isinstance (fname, basestring) \ and Os.path.exists (fname): With open (fname, "RB") as FH: For chunk in Read_chunks (FH): M.update (chunk) #上传的文件缓存 or open file stream elif Fname.__clas s__.__name__ in ["Stringio", "StringO"]/or Isinstance (fname, file): for chunk in Read_chunks (fname ): M.update(chunk) Else:return "" Return M.hexdigest ()