I'm working on existing python code that uses the request module to perform get/post towards a web site.
The python code also allow the use of proxies when a proxy is passed to the script otherwise no proxies are used.
My problem regards the use of proxies that requires authentication.
proxy = user:password@ip:port
self.s = requests.Session()
if proxy != "":
self.proxies = {
"http": "http://" + proxy,
"https": "https://" + proxy
}
self.s.proxies.update(proxies)
self.s.get('http://checkip.dyndns.org')
In the code above, if the proxy is configured each get is refused because no authentication is provided.
I found a solution that implies the use of HTTPProxyAuth.
This is the solution that I found:
import requests
s = requests.Session()
proxies = {
"http": "http://ip:port",
"https": "https://ip:port"
}
auth = HTTPProxyAuth("user", "pwd")
ext_ip = s.get('http://checkip.dyndns.org', proxies=proxies, auth=auth)
print ext_ip.text
My question is: is it possibile to set the proxy authorization globally instead of modify each s.get in the code? Because sometime the script could be used without proxy and sometimes with proxy.
Thanks in advance!
Best Regards, Federico