Using an HTTP proxy¶
Note
Support for HTTP proxies is only available in Libcloud trunk and higher.
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 below:
- By setting
http_proxy
environment variable (this setting is system / process wide) - By passing
http_proxy
argument to thelibcloud.common.base.LibcloudConnection
class constructor (this setting is local to the connection instance) - By calling
libcloud.common.base.LibcloudConnection.set_http_proxy()
method (this setting is local to the connection instance)
Known limitations¶
- Only HTTP basic access authentication proxy authorization method is supported
Examples¶
This section includes some code examples which show how to use an HTTP proxy with Libcloud.
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:
http_proxy=http://<proxy hostname>:<proxy port> python my_script.py
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 constructor¶
By passing http_proxy
argument to the
libcloud.common.base.Connection
class constructor, you can specify
which proxy to use for a particular connection.
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
PROXY_URL_NO_AUTH_1 = 'http://<proxy hostname 1>:<proxy port 2>'
cls = get_driver(Provider.RACKSPACE)
driver = cls('username', 'api key', region='ord',
http_proxy=PROXY_URL_NO_AUTH_1)
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 below.
from pprint import pprint
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
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')
# 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())