Skip to content

Commit 5035827

Browse files
authored
Merge pull request #5670 from smarie/pr_proxy_conf_helper_and_doc
Proxy related doc updates
2 parents 589c454 + f02a80c commit 5035827

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,4 @@ Patches and Suggestions
190190
- Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)
191191
- "Dull Bananas" <[email protected]> (`@dullbananas <https://github.com/dullbananas>`_)
192192
- Alessio Izzo (`@aless10 <https://github.com/aless10>`_)
193+
- Sylvain Marié (`@smarie <https://github.com/smarie>`_)

docs/user/advanced.rst

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,26 @@ If you need to use a proxy, you can configure individual requests with the
589589

590590
requests.get('http://example.org', proxies=proxies)
591591

592-
You can also configure proxies by setting the environment variables
593-
``HTTP_PROXY`` and ``HTTPS_PROXY``.
592+
Alternatively you can configure it once for an entire
593+
:class:`Session <requests.Session>`::
594594

595-
::
595+
import requests
596+
597+
proxies = {
598+
'http': 'http://10.10.1.10:3128',
599+
'https': 'http://10.10.1.10:1080',
600+
}
601+
session = request.Session()
602+
session.proxies.update(proxies)
603+
604+
session.get('http://example.org')
605+
606+
When the proxies configuration is not overridden in python as shown above,
607+
by default Requests relies on the proxy configuration defined by standard
608+
environment variables ``http_proxy``, ``https_proxy``, ``no_proxy`` and
609+
``curl_ca_bundle``. Uppercase variants of these variables are also supported.
610+
You can therefore set them to configure Requests (only set the ones relevant
611+
to your needs)::
596612

597613
$ export HTTP_PROXY="http://10.10.1.10:3128"
598614
$ export HTTPS_PROXY="http://10.10.1.10:1080"
@@ -601,9 +617,17 @@ You can also configure proxies by setting the environment variables
601617
>>> import requests
602618
>>> requests.get('http://example.org')
603619

604-
To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax::
620+
To use HTTP Basic Auth with your proxy, use the `http://user:password@host/`
621+
syntax in any of the above configuration entries::
605622

606-
proxies = {'http': 'http://user:[email protected]:3128/'}
623+
$ export HTTPS_PROXY="http://user:[email protected]:1080"
624+
625+
$ python
626+
>>> proxies = {'http': 'http://user:[email protected]:3128/'}
627+
628+
.. warning:: Storing sensitive username and password information in an
629+
environment variable or a version-controled file is a security risk and is
630+
highly discouraged.
607631

608632
To give a proxy for a specific scheme and host, use the
609633
`scheme://hostname` form for the key. This will match for
@@ -615,6 +639,23 @@ any request to the given scheme and exact hostname.
615639

616640
Note that proxy URLs must include the scheme.
617641

642+
Finally, note that using a proxy for https connections typically requires your
643+
local machine to trust the proxy's root certificate. By default the list of
644+
certificates trusted by Requests can be found with::
645+
646+
from requests.utils import DEFAULT_CA_BUNDLE_PATH
647+
print(DEFAULT_CA_BUNDLE_PATH)
648+
649+
You override this default certificate bundle by setting the standard
650+
``curl_ca_bundle`` environment variable to another file path::
651+
652+
$ export curl_ca_bundle="/usr/local/myproxy_info/cacert.pem"
653+
$ export https_proxy="http://10.10.1.10:1080"
654+
655+
$ python
656+
>>> import requests
657+
>>> requests.get('https://example.org')
658+
618659
SOCKS
619660
^^^^^
620661

0 commit comments

Comments
 (0)