如何使用 python 的请求模块发送多个文件并为每个文件自定义 header?
How to send multiple files using python's requests module and customize the header for each file?
从 http://docs.python-requests.org/en/latest/user/quickstart/ 开始,我可以找到为单个 multipart-encoded 文件自定义 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/,我可以找到发送多个multipart-encoded个文件的方法:
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'))]
假设我想发送上面两张图片,但我想为第二张图片自定义header。一个合理的尝试是:
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 请求的样子,所有 headers 都在开头,除了 content-type
和 content-disposition
用于多部分内容(它们描述内容)和它们仅在顶级 content-type
为 multipart/form-data
:
时适用
POST /test HTTP/1.1
Host: host
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Accept: text/html
Accept-Language: en-us
Accept-Charset: utf-8
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------3141592654
Content-Length: 111
-----------------------------3141592654
Content-Disposition: form-data; name="image"; filename="foo.png"
Content-Type: image/png
[img-data]
-----------------------------3141592654
Content-Disposition: form-data; name="image"; filename="bar.png"
Content-Type: image/png
[img-data]
从 http://docs.python-requests.org/en/latest/user/quickstart/ 开始,我可以找到为单个 multipart-encoded 文件自定义 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/,我可以找到发送多个multipart-encoded个文件的方法:
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'))]
假设我想发送上面两张图片,但我想为第二张图片自定义header。一个合理的尝试是:
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 请求的样子,所有 headers 都在开头,除了 content-type
和 content-disposition
用于多部分内容(它们描述内容)和它们仅在顶级 content-type
为 multipart/form-data
:
POST /test HTTP/1.1 Host: host User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36 Accept: text/html Accept-Language: en-us Accept-Charset: utf-8 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------3141592654 Content-Length: 111 -----------------------------3141592654 Content-Disposition: form-data; name="image"; filename="foo.png" Content-Type: image/png [img-data] -----------------------------3141592654 Content-Disposition: form-data; name="image"; filename="bar.png" Content-Type: image/png [img-data]