You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
urllib2 cannnot handle https and BasicAuth via Proxy. #61720
I found a similar case at http://bugs.python.org/issue7291. However, this issue indicates a case of authentication at a proxy server. In my case, the origin server uses basic authentication.
The origin server address is not set in Request URI in the https request in case of using proxy servers, because https request gets encripted with SSL when it passes through proxies.
urllib2 works well for the first on-going request and in-comming return. However, after the first in-comming return (401 code), urllib2 module sends a worng request that includes the origin server URL.
I suggest modification for set_proxy method in Request class.
Original code:
def set_proxy(self, host, type):
if self.type == 'https' and not self._tunnel_host:
self._tunnel_host = self.host
else:
self.type = type
self.__r_host = self.__original
self.host=host
As a value is already set to tunnel_host when the second out-going request is sent, "self.type == 'https' and not self._tunnel_host" is false.
Code suggested:
def set_proxy(self, host, type):
if self.type == 'https' and not self._tunnel_host:
self._tunnel_host = self.host
else:
self.type = type
if self.type != 'https':
self.__r_host = self.__original
Activity
masatokawamura commentedon Mar 22, 2013
When urllib2 module is used, https connections to servers that BasicAuth is required, via proxies, can not be established properly.
Sample code:
import urllib2
"opener.open" method throws an exception.
I found a similar case at http://bugs.python.org/issue7291. However, this issue indicates a case of authentication at a proxy server. In my case, the origin server uses basic authentication.
The origin server address is not set in Request URI in the https request in case of using proxy servers, because https request gets encripted with SSL when it passes through proxies.
urllib2 works well for the first on-going request and in-comming return. However, after the first in-comming return (401 code), urllib2 module sends a worng request that includes the origin server URL.
I suggest modification for set_proxy method in Request class.
Original code:
def set_proxy(self, host, type):
if self.type == 'https' and not self._tunnel_host:
self._tunnel_host = self.host
else:
self.type = type
self.__r_host = self.__original
As a value is already set to tunnel_host when the second out-going request is sent, "self.type == 'https' and not self._tunnel_host" is false.
Code suggested:
def set_proxy(self, host, type):
if self.type == 'https' and not self._tunnel_host:
self._tunnel_host = self.host
else:
self.type = type
if self.type != 'https':
self.__r_host = self.__original
regards.
BreamoreBoy commentedon Oct 3, 2014
Slipped under the radar? Note the patch suggested in msg184958.
crazymerlyn commentedon Aug 25, 2024
Should this be closed since this only affects Python 2 which is way past it's EOL? The python3 issue #42870 has been fixed.
orsenthil commentedon Nov 10, 2026
This will be handled by this - #161596