gpt4 book ai didi

python - 使用 werkzeug 从文件中解析和保存多部分表单数据

转载 作者:行者123 更新时间:2023-12-01 03:01:08 27 4
gpt4 key购买 nike

我有以下从 http POST 保存的文件,我想使用 werkzeug parse_form_data() 使用 Python 进行解析。请注意,我不是通过请求而是通过文件获取它。由于其他原因,我无法直接从 flask 请求中获取文件。由于我一直在使用 Flask,所以我尝试使用 werkzeug 来做到这一点。我以为我对无关连字符“--”的边界有问题,但我在以下测试文件中将所有内容修剪为非常简单的格式:

这是文件系统上的文件(myinputfile):

--806243354728155036129379
Content-Disposition: form-data; name="myfile"; filename="text.py"
Content-Type: application/octet-stream

some text in a file

--806243354728155036129379
Content-Disposition: form-data; name="field1"

abcde
--806243354728155036129379
Content-Disposition: form-data; name="field2"
123456678
--806243354728155036129379--

这是我使用的代码:

from werkzeug import parse_form_data
import io

inputfile = 'myinputfile'
content_type = 'Content-Type: multipart/form-data; boundary=806243354728155036129379'


environ = {
'wsgi.input': io.open(inputfile, 'rb'),
'CONTENT_LENGTH': '',
'CONTENT_TYPE': content_type,
'REQUEST_METHOD': 'POST'}

stream, form, files = parse_form_data(environ, silent=False)

我不断收到此错误:

File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 92, in parse_form_data
cls, silent).parse_from_environ(environ)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 171, in parse_from_environ
content_length, options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 195, in parse
content_length, options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 100, in wrapper
return f(self, stream, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 212, in _parse_multipart
form, files = parser.parse(stream, boundary, content_length)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 519, in parse
return self.cls(form), self.cls(files)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/datastructures.py", line 406, in __init__
for key, value in mapping or ():
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 517, in <genexpr>
form = (p[1] for p in formstream if p[0] == 'form')
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 476, in parse_parts
for ellt, ell in self.parse_lines(file, boundary, content_length):
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 395, in parse_lines
self.fail('Expected boundary at start of multipart data')
File "/usr/local/lib/python2.7/dist-packages/werkzeug/formparser.py", line 327, in fail
raise ValueError(message)
ValueError: Expected boundary at start of multipart data

最终我希望能够保存文件(可以是二进制的),并从字典中获取表单数据“Field1”和“Field2”。有任何想法吗?我也愿意使用其他方法。

最佳答案

好吧,我明白了。最初我以为内容长度可以留空。它的工作原理是指定长度,如下所示:

这是我使用的最终代码:

def fileuploaded():
import io

# print 'request headers {}'.format(request.headers)
rawfilename = request.headers.get('X_File')
content_type = request.headers.get('Content-Type')
print 'rawfilename = {}'.format(rawfilename)
print 'content_type= {}'.format(content_type)

# build the environ with the file stream
environ = {
'wsgi.input': io.open(rawfilename, 'rb'),
'CONTENT_LENGTH': os.path.getsize(rawfilename),
'CONTENT_TYPE': content_type,
'REQUEST_METHOD': 'POST'}

stream, form, files = parse_form_data(environ, silent=False)
f = files.get('infile')
f.save('/newfilepath/{}'.format(f.filename))

关于python - 使用 werkzeug 从文件中解析和保存多部分表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43827471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com