Repository: libcloud Updated Branches: refs/heads/trunk 816f3705e -> 06707ef0c
Update proxy support so it also works with Python 2.6. Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/06707ef0 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/06707ef0 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/06707ef0 Branch: refs/heads/trunk Commit: 06707ef0c8c52555f10ce9152388e6416a43fac4 Parents: 816f370 Author: Tomaz Muraus <[email protected]> Authored: Thu Aug 21 12:33:22 2014 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Thu Aug 21 12:44:03 2014 +0200 ---------------------------------------------------------------------- .../http_proxy/set_http_proxy_method.py | 11 ++++- docs/other/using-http-proxy.rst | 42 +++++++++++++++----- libcloud/httplib_ssl.py | 14 ++++--- libcloud/test/test_connection.py | 7 ---- 4 files changed, 49 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/06707ef0/docs/examples/http_proxy/set_http_proxy_method.py ---------------------------------------------------------------------- diff --git a/docs/examples/http_proxy/set_http_proxy_method.py b/docs/examples/http_proxy/set_http_proxy_method.py index b9d5bdf..9cebf97 100644 --- a/docs/examples/http_proxy/set_http_proxy_method.py +++ b/docs/examples/http_proxy/set_http_proxy_method.py @@ -3,10 +3,17 @@ from pprint import pprint from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver -PROXY_URL_NO_AUTH = 'http://<proxy hostname>:<proxy port>' +PROXY_URL_NO_AUTH_1 = 'http://<proxy hostname 1>:<proxy port 2>' +PROXY_URL_NO_AUTH_2 = 'http://<proxy hostname 1>:<proxy port 2>' PROXY_URL_BASIC_AUTH = 'http://<user>:<pass>@<proxy hostname>:<proxy port>' cls = get_driver(Provider.RACKSPACE) driver = cls('username', 'api key', region='ord') -driver.connection.set_http_proxy(proxy_url=PROXY_URL_NO_AUTH) + +# Use proxy 1 for this request +driver.connection.set_http_proxy(proxy_url=PROXY_URL_NO_AUTH_1) +pprint(driver.list_nodes()) + +# Use proxy 2 for this request +driver.connection.set_http_proxy(proxy_url=PROXY_URL_NO_AUTH_2) pprint(driver.list_nodes()) http://git-wip-us.apache.org/repos/asf/libcloud/blob/06707ef0/docs/other/using-http-proxy.rst ---------------------------------------------------------------------- diff --git a/docs/other/using-http-proxy.rst b/docs/other/using-http-proxy.rst index 4231bea..b59204b 100644 --- a/docs/other/using-http-proxy.rst +++ b/docs/other/using-http-proxy.rst @@ -5,9 +5,16 @@ Using an HTTP proxy Support for HTTP proxies is only available in Libcloud trunk and higher. -Libcloud supports using an HTTP proxy for outgoing HTTP and HTTPS requests. At -the moment, using a proxy is only supported if you are using Python 2.7 or -above (it has been tested with 2.7, PyPy, 3.1, 3.3, 3.3, 3.4). +Libcloud supports using an HTTP proxy for outgoing HTTP and HTTPS requests. + +Proxy support has been tested with the following Python versions; + +* Python 2.6 +* Python 2.7 / PyPy +* Python 3.1 +* Python 3.2 +* Python 3.3 +* Python 3.4 You can specify which HTTP proxy to use using one of the approaches described bellow: @@ -23,8 +30,7 @@ bellow: Known limitations ----------------- -* Python 2.6 is not supported -* Only HTTP basic authentication proxy authorization method is supported +* Only HTTP basic access authentication proxy authorization method is supported Examples -------- @@ -32,8 +38,12 @@ Examples This section includes some code examples which show how to use an HTTP proxy with Libcloud. -1. Using http_proxy environment variable -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +1. Using ``http_proxy`` environment variable +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By setting ``http_proxy`` environment variable you can specify which proxy to +use for all of the outgoing requests for a duration / life-time of the process +or a script. Without authentication: @@ -47,14 +57,24 @@ With basic auth authentication: http_proxy=http://<username>:<password>@<proxy hostname>:<proxy port> python my_script.py -2. Passing http_proxy argument to the connection class -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +2. Passing ``http_proxy`` argument to the connection class constructor +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By passing ``http_proxy`` argument to the +:class:`libcloud.common.base.Connection` class constructor, you can specify +which proxy to use for a particular connection. .. literalinclude:: /examples/http_proxy/constructor_argument.py :language: python -3. Calling set_http_proxy method -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +3. Calling ``set_http_proxy`` method +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Calling ``set_http_proxy`` method allows you to specify which proxy to use +for all the outgoing requests which follow ``set_http_proxy`` method call. + +This method also allows you to use a different proxy for each request as shown +in the example bellow. .. literalinclude:: /examples/http_proxy/set_http_proxy_method.py :language: python http://git-wip-us.apache.org/repos/asf/libcloud/blob/06707ef0/libcloud/httplib_ssl.py ---------------------------------------------------------------------- diff --git a/libcloud/httplib_ssl.py b/libcloud/httplib_ssl.py index d74ad40..4497da3 100644 --- a/libcloud/httplib_ssl.py +++ b/libcloud/httplib_ssl.py @@ -19,7 +19,6 @@ verification, depending on libcloud.security settings. import os import re import socket -import sys import ssl import base64 import warnings @@ -65,9 +64,6 @@ class LibcloudBaseConnection(object): basic auth authentication information. :type proxy_url: ``str`` """ - if sys.version_info[:2] == (2, 6): - raise Exception('HTTP proxy support requires Python 2.7 or higher') - result = self._parse_proxy_url(proxy_url=proxy_url) scheme = result[0] host = result[1] @@ -138,7 +134,15 @@ class LibcloudBaseConnection(object): auth_header = 'Basic %s' % (encoded.decode('utf-8')) headers['Proxy-Authorization'] = auth_header - self.set_tunnel(host=self.host, port=self.port, headers=headers) + if hasattr(self, 'set_tunnel'): + # Python 2.7 and higher + self.set_tunnel(host=self.host, port=self.port, headers=headers) + elif hasattr(self, '_set_tunnel'): + # Python 2.6 + self._set_tunnel(host=self.host, port=self.port, headers=headers) + else: + raise ValueError('Unsupported Python version') + self._set_hostport(host=self.proxy_host, port=self.proxy_port) def _activate_http_proxy(self, sock): http://git-wip-us.apache.org/repos/asf/libcloud/blob/06707ef0/libcloud/test/test_connection.py ---------------------------------------------------------------------- diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py index aabbd1d..c97cfb4 100644 --- a/libcloud/test/test_connection.py +++ b/libcloud/test/test_connection.py @@ -25,14 +25,10 @@ from libcloud.common.base import Connection from libcloud.common.base import LoggingConnection from libcloud.httplib_ssl import LibcloudBaseConnection from libcloud.httplib_ssl import LibcloudHTTPConnection -from libcloud.utils.py3 import PY26 class BaseConnectionClassTestCase(unittest.TestCase): def test_parse_proxy_url(self): - if PY26: - return - conn = LibcloudBaseConnection() proxy_url = 'http://127.0.0.1:3128' @@ -64,9 +60,6 @@ class BaseConnectionClassTestCase(unittest.TestCase): proxy_url=proxy_url) def test_constructor(self): - if PY26: - return - conn = LibcloudHTTPConnection(host='localhost', port=80) self.assertEqual(conn.proxy_scheme, None) self.assertEqual(conn.proxy_host, None)
