使用python cgi上傳文件並計算m
- 2020 年 1 月 13 日
- 筆記
對文件拷貝後進行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/htmln <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/htmln <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,顯示上傳成功
