我想使用代码here通过Python CGI脚本使用单个表单字段上载多个文件。较新的浏览器似乎按here和here支持此功能。 HTML表单似乎很简单:
<input name="file" type="file" multiple="" />
使用此表单选择两个文件名称file0
和file1
将导致每个HTML5 input multiple attribute如下:
file=file0&file=file1
我最初以为它将是一个数组,但它似乎使用与号分隔。
我尝试使用以下代码修改代码并添加for
语句以遍历form字段中指定的每个文件的尝试均未成功(请参见下面的错误)。如果使用for
语句不是最好的方法,那么我想其他的想法也可以使用Python。
#!/usr/bin/python
import cgi, os
form = cgi.FieldStorage()
# Generator to buffer file chunks
def fbuffer(f, chunk_size=10000):
while True:
chunk = f.read(chunk_size)
if not chunk: break
yield chunk
for fileitem in form['file']:
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
f = open('/var/www/domain.com/files' + fn, '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,)
单个文件选择错误:
Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/cgi-bin/test.py", line 13, in <module>, referer: https://www.domain.com/files/upload.htm
for fileitem in form['file']:, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/python2.6/cgi.py", line 518, in __iter__, referer: https://www.domain.com/files/upload.htm
return iter(self.keys()), referer: https://www.domain.com/files/upload.htm
File "/usr/lib/python2.6/cgi.py", line 583, in keys, referer: https://www.domain.com/files/upload.htm
raise TypeError, "not indexable", referer: https://www.domain.com/files/upload.htm
TypeError: not indexable, referer: https://www.domain.com/files/upload.htm
Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
两个文件选择错误:
Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/cgi-bin/test.py", line 19, in <module>, referer: https://www.domain.com/files/upload.htm
if fileitem.filename:, referer: https://www.domain.com/files/upload.htm
AttributeError: 'list' object has no attribute 'filename', referer: https://www.domain.com/files/upload.htm
Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
如果除去.filename
引用,则会产生第三个错误,与选择单个或两个文件相同:
Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/cgi-bin/test.py", line 24, in <module>, referer: https://www.domain.com/files/upload.htm
fn = os.path.basename(fileitem), referer: https://www.domain.com/files/upload.htm
File "/usr/lib/python2.6/posixpath.py", line 111, in basename, referer: https://www.domain.com/files/upload.htm
i = p.rfind('/') + 1, referer: https://www.domain.com/files/upload.htm
AttributeError: 'list' object has no attribute 'rfind', referer: https://www.domain.com/files/upload.htm
Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
参考方案
删除for file in form
。该错误表明form['file']
是一个列表。
添加到html格式:method=post enctype=multipart/form-data
。
import shutil
if 'file' in form:
filefield = form['file']
if not isinstance(filefield, list):
filefield = [filefield]
for fileitem in filefield:
if fileitem.filename:
fn = secure_filename(fileitem.filename)
# save file
with open('/var/www/domain.com/files/' + fn, 'wb') as f:
shutil.copyfileobj(fileitem.file, f)
如何访问页面内的第三方跟踪参数(通过Python)? - python我已经搜索了很多,到目前为止,还无法确定如何访问页面中的第三方跟踪参数。使用下面的代码,我可以通过python request软件包访问页面的cookie,但是仅对于本地域,不会返回任何第三方的cookie /域:import requests # create session s = requests.session() # read data from …
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
如何用'-'解析字符串到节点js本地脚本? - python我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…