如果你发送一个非常大的文件作为 multipart/form-data 请求,你可能希望将请求做成数据流。
默认下 requests 不支持, 但有个第三方包 requests-toolbelt 是支持的。你可以阅读 toolbelt 文档 来了解使用方法。
node2:/etc/init.d#pip install requests-toolbelt
Collecting requests-toolbelt
/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Exception:
Traceback (most recent call last):
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 324, in run
requirement_set.prepare_files(finder)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 620, in _prepare_file
session=self.session, hashes=hashes)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/download.py", line 821, in unpack_url
hashes=hashes
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/download.py", line 659, in unpack_http_url
hashes)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/download.py", line 853, in _download_http_url
stream=True,
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/sessions.py", line 488, in get
return self.request('GET', url, **kwargs)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/download.py", line 386, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/cachecontrol/adapter.py", line 47, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
You are using pip version 9.0.1, however version 10.0.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
下载最新的证书文件
wget http://curl.haxx.se/ca/cacert.pem
更名为ca-bundle.crt放置到默认目录
mv cacert.pem ca-bundle.crt
mv ca-bundle.crt /etc/pki/tls/certs
node2:/etc/init.d#wget http://curl.haxx.se/ca/cacert.pem
--2018-04-14 17:10:49-- http://curl.haxx.se/ca/cacert.pem
Resolving curl.haxx.se... 151.101.74.49
Connecting to curl.haxx.se|151.101.74.49|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://curl.haxx.se/ca/cacert.pem [following]
--2018-04-14 17:11:05-- https://curl.haxx.se/ca/cacert.pem
Connecting to curl.haxx.se|151.101.74.49|:443... connected.
ERROR: certificate common name “c.sni.fastly.net” doesn’t match requested host name “curl.haxx.se”.
To connect to curl.haxx.se insecurely, use ‘--no-check-certificate’.
node2:/etc/init.d#wget http://curl.haxx.se/ca/cacert.pem --no-check-certificate
--2018-04-14 17:11:14-- http://curl.haxx.se/ca/cacert.pem
Resolving curl.haxx.se... 151.101.74.49
Connecting to curl.haxx.se|151.101.74.49|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://curl.haxx.se/ca/cacert.pem [following]
--2018-04-14 17:11:20-- https://curl.haxx.se/ca/cacert.pem
Connecting to curl.haxx.se|151.101.74.49|:443... connected.
WARNING: certificate common name “c.sni.fastly.net” doesn’t match requested host name “curl.haxx.se”.
HTTP request sent, awaiting response... 200 OK
Length: 215556 (211K) [application/x-pem-file]
Saving to: “cacert.pem”
100%[=========================================================================================================================>] 215,556 411K/s in 0.5s
2018-04-14 17:11:20 (411 KB/s) - “cacert.pem” saved [215556/215556]
node2:/etc/init.d#
实现解决不了,手动下载安装
node2:/root#easy_install requests-toolbelt
Searching for requests-toolbelt
Reading https://pypi.python.org/simple/requests-toolbelt/
Downloading https://files.pythonhosted.org/packages/86/f9/e80fa23edca6c554f1994040064760c12b51daff54b55f9e379e899cd3d4/requests-toolbelt-0.8.0.tar.gz#sha256=f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5
error: Download error for https://files.pythonhosted.org/packages/86/f9/e80fa23edca6c554f1994040064760c12b51daff54b55f9e379e899cd3d4/requests-toolbelt-0.8.0.tar.gz#sha256=f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
node2:/root#
node2:/root/certifi-2018.4.16#python setup.py install
/usr/local/python27/lib/python2.7/site-packages/setuptools-33.1.1-py2.7.egg/setuptools/dist.py:333: UserWarning: Normalizing '2018.04.16' to '2018.4.16'
running install
running bdist_egg
running egg_info
writing certifi.egg-info/PKG-INFO
writing top-level names to certifi.egg-info/top_level.txt
writing dependency_links to certifi.egg-info/dependency_links.txt
reading manifest file 'certifi.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'certifi.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/certifi
copying certifi/__init__.py -> build/lib/certifi
copying certifi/__main__.py -> build/lib/certifi
copying certifi/core.py -> build/lib/certifi
copying certifi/cacert.pem -> build/lib/certifi
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/certifi
copying build/lib/certifi/cacert.pem -> build/bdist.linux-x86_64/egg/certifi
copying build/lib/certifi/__init__.py -> build/bdist.linux-x86_64/egg/certifi
copying build/lib/certifi/__main__.py -> build/bdist.linux-x86_64/egg/certifi
copying build/lib/certifi/core.py -> build/bdist.linux-x86_64/egg/certifi
byte-compiling build/bdist.linux-x86_64/egg/certifi/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/certifi/__main__.py to __main__.pyc
byte-compiling build/bdist.linux-x86_64/egg/certifi/core.py to core.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying certifi.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying certifi.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying certifi.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying certifi.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying certifi.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating dist
creating 'dist/certifi-2018.4.16-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing certifi-2018.4.16-py2.7.egg
creating /usr/local/python27/lib/python2.7/site-packages/certifi-2018.4.16-py2.7.egg
Extracting certifi-2018.4.16-py2.7.egg to /usr/local/python27/lib/python2.7/site-packages
Adding certifi 2018.4.16 to easy-install.pth file
Installed /usr/local/python27/lib/python2.7/site-packages/certifi-2018.4.16-py2.7.egg
Processing dependencies for certifi==2018.4.16
Finished processing dependencies for certifi==2018.4.16
node2:/root#cd requests-toolbelt-0.8.0
node2:/root/requests-toolbelt-0.8.0#python setup.py install
running install
running bdist_egg
running egg_info
writing requirements to requests_toolbelt.egg-info/requires.txt
writing requests_toolbelt.egg-info/PKG-INFO
writing top-level names to requests_toolbelt.egg-info/top_level.txt
writing dependency_links to requests_toolbelt.egg-info/dependency_links.txt
reading manifest file 'requests_toolbelt.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
no previously-included directories found matching 'docs/_build'
warning: no previously-included files matching '*.py[cdo]' found anywhere in distribution
warning: no previously-included files matching '__pycache__' found anywhere in distribution
warning: no previously-included files matching '*.so' found anywhere in distribution
warning: no previously-included files matching '*.pyd' found anywhere in distribution
writing manifest file 'requests_toolbelt.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/requests_toolbelt
creating build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/host_header_ssl.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/fingerprint.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/appengine.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/ssl.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/socket_options.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
copying build/lib/requests_toolbelt/adapters/source.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/adapters
creating build/bdist.linux-x86_64/egg/requests_toolbelt/auth
copying build/lib/requests_toolbelt/auth/http_proxy_digest.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/auth
copying build/lib/requests_toolbelt/auth/_digest_auth_compat.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/auth
copying build/lib/requests_toolbelt/auth/handler.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/auth
copying build/lib/requests_toolbelt/auth/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/auth
copying build/lib/requests_toolbelt/auth/guess.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/auth
copying build/lib/requests_toolbelt/_compat.py -> build/bdist.linux-x86_64/egg/requests_toolbelt
creating build/bdist.linux-x86_64/egg/requests_toolbelt/threaded
copying build/lib/requests_toolbelt/threaded/thread.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/threaded
copying build/lib/requests_toolbelt/threaded/pool.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/threaded
copying build/lib/requests_toolbelt/threaded/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/threaded
creating build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils
copying build/lib/requests_toolbelt/downloadutils/stream.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils
copying build/lib/requests_toolbelt/downloadutils/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils
copying build/lib/requests_toolbelt/downloadutils/tee.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils
creating build/bdist.linux-x86_64/egg/requests_toolbelt/multipart
copying build/lib/requests_toolbelt/multipart/decoder.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/multipart
copying build/lib/requests_toolbelt/multipart/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/multipart
copying build/lib/requests_toolbelt/multipart/encoder.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/multipart
copying build/lib/requests_toolbelt/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt
creating build/bdist.linux-x86_64/egg/requests_toolbelt/cookies
copying build/lib/requests_toolbelt/cookies/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/cookies
copying build/lib/requests_toolbelt/cookies/forgetful.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/cookies
copying build/lib/requests_toolbelt/exceptions.py -> build/bdist.linux-x86_64/egg/requests_toolbelt
copying build/lib/requests_toolbelt/sessions.py -> build/bdist.linux-x86_64/egg/requests_toolbelt
copying build/lib/requests_toolbelt/streaming_iterator.py -> build/bdist.linux-x86_64/egg/requests_toolbelt
creating build/bdist.linux-x86_64/egg/requests_toolbelt/utils
copying build/lib/requests_toolbelt/utils/deprecated.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/utils
copying build/lib/requests_toolbelt/utils/formdata.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/utils
copying build/lib/requests_toolbelt/utils/user_agent.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/utils
copying build/lib/requests_toolbelt/utils/__init__.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/utils
copying build/lib/requests_toolbelt/utils/dump.py -> build/bdist.linux-x86_64/egg/requests_toolbelt/utils
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/host_header_ssl.py to host_header_ssl.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/fingerprint.py to fingerprint.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/appengine.py to appengine.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/ssl.py to ssl.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/socket_options.py to socket_options.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/adapters/source.py to source.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/auth/http_proxy_digest.py to http_proxy_digest.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/auth/_digest_auth_compat.py to _digest_auth_compat.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/auth/handler.py to handler.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/auth/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/auth/guess.py to guess.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/_compat.py to _compat.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/threaded/thread.py to thread.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/threaded/pool.py to pool.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/threaded/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils/stream.py to stream.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/downloadutils/tee.py to tee.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/multipart/decoder.py to decoder.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/multipart/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/multipart/encoder.py to encoder.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/cookies/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/cookies/forgetful.py to forgetful.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/exceptions.py to exceptions.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/sessions.py to sessions.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/streaming_iterator.py to streaming_iterator.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/utils/deprecated.py to deprecated.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/utils/formdata.py to formdata.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/utils/user_agent.py to user_agent.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/utils/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/requests_toolbelt/utils/dump.py to dump.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying requests_toolbelt.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying requests_toolbelt.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying requests_toolbelt.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying requests_toolbelt.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying requests_toolbelt.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist/requests_toolbelt-0.8.0-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing requests_toolbelt-0.8.0-py2.7.egg
Removing /usr/local/python27/lib/python2.7/site-packages/requests_toolbelt-0.8.0-py2.7.egg
Copying requests_toolbelt-0.8.0-py2.7.egg to /usr/local/python27/lib/python2.7/site-packages
requests-toolbelt 0.8.0 is already the active version in easy-install.pth
Installed /usr/local/python27/lib/python2.7/site-packages/requests_toolbelt-0.8.0-py2.7.egg
Processing dependencies for requests-toolbelt==0.8.0
Searching for requests==2.18.4
Best match: requests 2.18.4
Adding requests 2.18.4 to easy-install.pth file
Using /usr/local/python27/lib/python2.7/site-packages
Searching for certifi==2018.4.16
Best match: certifi 2018.4.16
Processing certifi-2018.4.16-py2.7.egg
certifi 2018.4.16 is already the active version in easy-install.pth
Using /usr/local/python27/lib/python2.7/site-packages/certifi-2018.4.16-py2.7.egg
Searching for chardet==3.0.4
Best match: chardet 3.0.4
Adding chardet 3.0.4 to easy-install.pth file
Installing chardetect script to /usr/local/python27/bin
Using /usr/local/python27/lib/python2.7/site-packages
Searching for idna==2.6
Best match: idna 2.6
Adding idna 2.6 to easy-install.pth file
Using /usr/local/python27/lib/python2.7/site-packages
Searching for urllib3==1.22
Best match: urllib3 1.22
Adding urllib3 1.22 to easy-install.pth file
Using /usr/local/python27/lib/python2.7/site-packages
Finished processing dependencies for requests-toolbelt==0.8.0
node2:/root/requests-toolbelt-0.8.0#
node2:/root#python
Python 2.7.3 (default, Mar 30 2017, 20:15:12)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests_toolbelt
>>> print requests_toolbelt.__file__
/usr/local/python27/lib/python2.7/site-packages/requests_toolbelt-0.8.0-py2.7.egg/requests_toolbelt/__init__.pyc
>>>