以下代码以串行方式将多个文件从浏览器上传到主机。为了使过程更快,如何并行处理文件以便将多个流发送到服务器?如果将文件发送到同一主机,这是否值得追求?浏览器是否允许多个上传流到同一主机?如果是这样,这将如何工作?
我认为不可能将文件分成几部分并将它们并行编写,类似于此示例,因为它们是由浏览器而不是 Python 客户端提交的。
#!/usr/bin/python
import cgi, os
import shutil
import cgitb; cgitb.enable() # for troubleshooting
form = cgi.FieldStorage()
print """\
Content-Type: text/html\n
<html><body>
"""
if 'file' in form:
filefield = form['file']
if not isinstance(filefield, list):
filefield = [filefield]
for fileitem in filefield:
if fileitem.filename:
fn = os.path.basename(fileitem.filename)
# save file
with open('/var/www/site/files/' + fn, 'wb') as f:
shutil.copyfileobj(fileitem.file, f)
# line breaks are not occuring between interations
print 'File "' + fn + '" was uploaded successfully<br/>'
message = 'All files uploaded'
else:
message = 'No file was uploaded'
print """
<p>%s</p>
</body></html>
""" % (message)