Facilitate proxy configuration by environment variables
authorJohannes Gorset <jgorset@gmail.com>
Thu, 8 Mar 2012 11:38:00 +0000 (12:38 +0100)
committerJohannes Gorset <jgorset@gmail.com>
Thu, 8 Mar 2012 12:34:23 +0000 (13:34 +0100)
docs/user/advanced.rst
requests/models.py

index aec9cacf3ce442af30ed2b4f7c32060802c70bf9..7c14ba14054ced668fe94386fdb2db33f1eb5a01 100644 (file)
@@ -291,3 +291,30 @@ To do so, just configure Requests with a stream to write to::
     >>> requests.get('http://httpbin.org/headers', config=my_config)
     2011-08-17T03:04:23.380175   GET   http://httpbin.org/headers
     <Response [200]>
+
+Proxies
+-------
+
+If you need to use a proxy, you can configure individual requests with the
+``proxies`` argument to any request method:
+
+::
+
+    import requests
+
+    proxies = {
+      "http": "10.10.1.10:3128"
+      "https": "10.10.1.10:1080"
+    }
+
+    requests.get("http://example.org", proxies=proxies)
+
+You can also configure proxies by environment variables ``HTTP_PROXY`` and ``HTTPS_PROXY``.
+
+::
+
+    $ export HTTP_PROXY="10.10.1.10:3128"
+    $ export HTTPS_PROXY="10.10.1.10:1080"
+    $ python
+    >>> import requests
+    >>> requests.get("http://example.org")
index 1b143b0e163d4e8b48fdbec40362a7488c6c391f..795c11551a7438988b239260b8bcfb459e4b3d55 100644 (file)
@@ -102,6 +102,14 @@ class Request(object):
         # Dictionary mapping protocol to the URL of the proxy (e.g. {'http': 'foo.bar:3128'})
         self.proxies = dict(proxies or [])
 
+        # If no proxies are given, allow configuration by environment variables
+        # HTTP_PROXY and HTTPS_PROXY.
+        if not self.proxies:
+          if 'HTTP_PROXY' in os.environ:
+            self.proxies['http'] = os.environ['HTTP_PROXY']
+          if 'HTTPS_PROXY' in os.environ:
+            self.proxies['https'] = os.environ['HTTPS_PROXY']
+
         self.data, self._enc_data = self._encode_params(data)
         self.params, self._enc_params = self._encode_params(params)