对文件拷贝后进行MD5值比较,看是不是拷贝完全。google和baidu上都是使用md5模块读取所有的文件进内存,在计算md5,导致计算超过1G大小的文件报错。增量计算MD5的方法:
#!/usr/bin/python
import hashlib
import sys
def main():
if len(sys.argv) != 2:
sys.exit('usage:%s file' % sys.argv[0])
filename = sys.argv[1]
md5 = hashlib.md5()
with open(filename,'rb') as fp:
while True:
blk = fp.read(4096)
if not blk:break
md5.update(blk)
print md5.hexdigest(),filename
if __name__ == '__main__':
main()
这个效果一样,使用md5,上面使用的hashlib:
#!/usr/bin/python
import md5
import sys
def main():
if len(sys.argv) != 2:
sys.exit('usage:%s file' % sys.argv[0])
filename = sys.argv[1]
m = md5.new()
with open(filename,'rb') as fp:
while True:
blk = fp.read(4096)
if not blk:break
m.update(blk)
print m.hexdigest(),filename
if __name__ == '__main__':
main()
——————————————————分割线——————————————————
上面例子的应用
计算上传文件的md5值,并显示文件大小:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import cgi, os
import cgitb; cgitb.enable()
import md5
form = cgi.FieldStorage()
fileitem = form['filename']
if fileitem.filename:
#fn = os.path.basename(fileitem.filename)
#open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'Got file %s"' % cgi.escape(fileitem.filename)
m = md5.new()
size = 0
while True:
data = fileitem.file.read(4096)
if not len(data):
break
size += len(data)
m.update(data)
#open('/tmp/' + fn, 'wb').write(data)
li = 'Received file of %d bytes. MD5sum is %s' % (size,m.hexdigest())
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
<p>%s</p>
</body>
</html>
""" % (message,li)
MD5.html:
<html>
<body>
<form enctype="multipart/form-data"
action="/op/md5_file.py" method="post">
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="md5" /></p>
</form>
</body>
</html>
效果:
点击md5,计算文件的md5值
——————————————————分割线——————————————————
跟上个例子类似,功能不一样
python cgi上传文件:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
# 获取文件名
fileitem = form['filename']
# 检测文件是否上传
if fileitem.filename:
# 设置文件路径
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
index.html
<html>
<body>
<form enctype="multipart/form-data"
action="/op/save_file.py" method="post">
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>
效果:
点击Upload,显示上传成功