Search code examples
pythonhttp-proxy

Python request with authenticated proxy without exposing username and password.


I am using request library to connect to an api and get data. I have the proxy to connect and it works with AuthenticatedProxy

This is how my code looks.

import requests
s = requests.Session()
url = "https://testapi"
urlkey = “testkey”
proxies = {'https': 'http://<UserName>:<Password>@proxy_url:8080'}
resp = s.get(url, params={'key':urlkey }, proxies = proxies)
content = resp.content
print content

Username and Password is exposed here and I want to avoid that. How can I acheived that ? Can i ask request to use defaultCredentials or 'account that is running the python script' credentials ?

In .net, following config works:

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
      <proxy proxyaddress="https://testapi:8080" bypassonlocal="True"/>
    </defaultProxy>
  </system.net>

Thanks all. Similar question here.


Solution

  • From the request docs:

    You can also configure proxies by setting the environment variables HTTP_PROXY and HTTPS_PROXY.

    So you could avoid revealing the credentials in the script by setting the environment variable instead:

    $ export HTTPS_PROXY="http://<UserName>:<Password>@proxy_url:8080"
    

    And then in the script you would use the proxy by just calling:

    resp = s.get(url, params={'key':urlkey })