How to pass proxy-authentication to python Requests module
I found the solution, about how to set proxy authentication globally:
import requests
import urllib2
import re
from requests.auth import HTTPProxyAuth
s = requests.Session()
proxies = {
"http": "http://185.165.193.208:8000",
"https": "https://185.165.193.208:8000"
}
auth = HTTPProxyAuth("gRRT7n", "NMu8g0")
s.proxies = proxies
s.auth = auth # Set authorization parameters globally
ext_ip = s.get('http://checkip.dyndns.org')
print ext_ip.text
BR, Federico
Federico
Updated on June 14, 2022Comments
-
Federico almost 2 years
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
-
CristiFati over 5 yearsYou should mark this answer as a solution to the question, if it resolved it.