gpt4 book ai didi

python - 如何使用python的requests模块发送多个文件并为每个文件自定义 header ?

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:32 25 4
gpt4 key购买 nike

来自http://docs.python-requests.org/en/latest/user/quickstart/ ,我可以找到为单个多部分编码文件自定义 header 的方法:

You can set the filename, content_type and headers explicitly:

files = {'file': ('report.xls', open('report.xls', 'rb'),
'application/vnd.ms-excel', {'Expires': '0'})}

来自http://docs.python-requests.org/en/latest/user/advanced/ ,我可以找到发送多个多部分编码文件的方法:

You can send multiple files in one request...To do that, just set files to a list of tuples of (form_field_name, file_info):

multiple_files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]

假设我想发送上面两张图片,但我想自定义第二张图片的标题。一个合理的尝试是:

multiple_files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
('images', ('bar.png', open('bar.png', 'rb'), 'image/png', {'Expires': '0'}))]

但是我收到以下错误:

In [49]: multiple_files = [('images', ('foo.png', "123", 'image/png')),
('images', ('bar.png', "123", 'image/png', {'Expires': '0'}))]

In [50]: response = requests.post(
url,
headers={'accept': 'application/json'},
files = multiple_files
)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-50-ef900c5109d7> in <module>()
2 url,
3 headers={'accept': 'application/json'},
----> 4 files = multiple_files
5 )

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/api.pyc in post(url, data, **kwargs)
86 """
87
---> 88 return request('post', url, data=data, **kwargs)
89
90

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/api.pyc in request(method, url, **kwargs)
42
43 session = sessions.Session()
---> 44 return session.request(method=method, url=url, **kwargs)
45
46

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert)
321 hooks = hooks,
322 )
--> 323 prep = self.prepare_request(req)
324
325 proxies = proxies or {}

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/sessions.pyc in prepare_request(self, request)
262 auth=merge_setting(auth, self.auth),
263 cookies=merged_cookies,
--> 264 hooks=merge_setting(request.hooks, self.hooks),
265 )
266 return p

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/models.pyc in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks)
281 self.prepare_headers(headers)
282 self.prepare_cookies(cookies)
--> 283 self.prepare_body(data, files)
284 self.prepare_auth(auth, url)
285 # Note that prepare_auth must be last to enable authentication schemes

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/models.pyc in prepare_body(self, data, files)
411 # Multi-part file uploads.
412 if files:
--> 413 (body, content_type) = self._encode_files(files, data)
414 else:
415 if data:

/Library/Python/2.7/site-packages/requests-2.0.0-py2.7.egg/requests/models.pyc in _encode_files(files, data)
124 fn, fp = v
125 else:
--> 126 fn, fp, ft = v
127 else:
128 fn = guess_filename(v) or k

ValueError: too many values to unpack

我的问题是:当存在多个文件时,我可以自定义单个文件的 header 吗?

最佳答案

这就是多部分 HTTP 请求的样子,除了多部分内容的 content-typecontent-disposition(它们描述内容),并且仅当顶级 content-typemultipart/form-data 时它们才适用:

POST /test HTTP/1.1Host: hostUser-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36Accept: text/htmlAccept-Language: en-usAccept-Charset: utf-8Keep-Alive: 300Connection: keep-aliveContent-Type: multipart/form-data; boundary=---------------------------3141592654Content-Length: 111-----------------------------3141592654Content-Disposition: form-data; name="image"; filename="foo.png"Content-Type: image/png[img-data]-----------------------------3141592654Content-Disposition: form-data; name="image"; filename="bar.png"Content-Type: image/png[img-data]

关于python - 如何使用python的requests模块发送多个文件并为每个文件自定义 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622312/

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