python cgi 上传大文件

Python  /  houtizong 发布于 3年前   243
#!E:\Python27\python.exeimport cgi, osimport cgitb; cgitb.enable()try: # Windows needs stdio set for binary mode.    import msvcrt    import uuid    msvcrt.setmode (0, os.O_BINARY) # stdin  = 0    msvcrt.setmode (1, os.O_BINARY) # stdout = 1except ImportError:    passform = cgi.FieldStorage()# Generator to buffer file chunksdef fbuffer(f, chunk_size=10000):   while True:      chunk = f.read(chunk_size)      if not chunk: break      yield chunk# A nested FieldStorage instance holds the filefileitem = form['file']# Test if the file was uploadedif fileitem.filename:   # strip leading path from file name to avoid directory traversal attacks   fn = os.path.basename(fileitem.filename)     # Internet Explorer will attempt to provide full path for filename fix   fn = fn.split('\\')[-1]    # This path must be writable by the web server in order to upload the file.   path = 'D:/upload/'   filepath = path + fn   # Open the file for writing    f = open(filepath , 'wb', 10000)   # Read the file in chunks   for chunk in fbuffer(fileitem.file):      f.write(chunk)   f.close()   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,)

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客