2

In my python (2.7) application I make use of a Cloudant database as follows:

from cloudant.client import Cloudant

client = Cloudant('XXX', 'YYY', account='ZZZ')
client.connect()
my_db = client['my_database']
for doc in my_db: print doc

The environment in which this application runs makes use of proxy.pac which can not be bypassed. How could I make the connection to Cloudant .pac aware or how could I let Cloudant automatically look for proxy.pac?

I've found the python package PyPac (https://pypac.readthedocs.io/en/latest/) but do not have the slightest idea how I should use this in the Cloudant context.

Thanks for your ideas and help in advance.

Bill Bridge
  • 821
  • 1
  • 9
  • 30
  • The comment I received from direct email: "python-cloudant does not support PAC files. The only proxy support in python-cloudant at the moment is that provided by the underlying requests library. We don't expose a way to customize the arguments on every request, but you should be able to configure a proxy address to be used via the HTTPS_PROXY environment variable, but this isn't something we've tested AFAIK." – Bill Bridge Jan 16 '18 at 17:25
  • Where is located the file "proxy.pac"? – Tiger-222 Jan 23 '18 at 13:26
  • Are you kidding? Can be anywhere. – Bill Bridge Jan 23 '18 at 15:47
  • OK, let's retry: do you know the exact location of the file in your environment? I am asking because you will need to kknow that before trying to use pypac. – Tiger-222 Jan 23 '18 at 17:08
  • In some cases you will know the location in others not. So PyPac provides automated pac finder functionality. So for this case let's assume we know the location. – Bill Bridge Jan 24 '18 at 07:52

1 Answers1

1

Since PyPAC 0.6.0, you can use the pac_context_for_url context manager:

from pypac import pac_context_for_url


with pac_context_for_url('https://acct.cloudant.com'):
    # Do your Cloudant stuff

Original answer:

This should work as expected:

import os

import pypac


# PyPAC will auto-discover the current PAC settings
pac = pypac.get_pac()

# Find the proxy for Cloudant (I use this domain but anything else would work too)
proxies = pac.find_proxy_for_url('https://acct.cloudant.com', 'cloudant.com')

# It will return something like: 'PROXY 4.5.6.7:8080; PROXY 7.8.9.10:8080'
# Here we take the 1st one:
proxy = 'http://{}/'.format(proxies.split()[1].rstrip(';'))

# Set proxy envars
os.environ['HTTP_PROXY'] = os.environ['HTTPS_PROXY'] = proxy

# And now try Cloudant stuff
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • Hmmm, sometimes life can be so easy! :) Looks good to me. Never came up this simple but effective solution. Thanks very much. – Bill Bridge Jan 24 '18 at 14:25