Use proxy for HTTPS requests as well

Use proxy handler/opener classes, instead of request.set_proxy, to avoid any potential bugs in older Python versions.
Use the HTTP proxy (if configured) for HTTPS requests as well.
This commit is contained in:
Junaid Loonat 2016-04-25 10:14:49 +02:00
parent 3879f30d47
commit 654d98c125

View File

@ -57,6 +57,7 @@ if minisix.PY2:
return urllib.urlencode(*args, **kwargs).encode()
from urllib2 import HTTPError, URLError
from urllib import splithost, splituser
from urllib2 import build_opener, install_opener, ProxyHandler
else:
from http.client import InvalidURL
from urllib.parse import urlsplit, urlunsplit, urlparse
@ -72,6 +73,7 @@ else:
return urllib.parse.urlencode(*args, **kwargs)
from urllib.error import HTTPError, URLError
from urllib.parse import splithost, splituser
from urllib.request import build_opener, install_opener, ProxyHandler
class Error(Exception):
pass
@ -145,7 +147,12 @@ def getUrlFd(url, headers=None, data=None, timeout=None):
request.add_data(data)
httpProxy = force(proxy)
if httpProxy:
request.set_proxy(httpProxy, 'http')
proxyHandler = ProxyHandler({
'http': httpProxy,
'https': httpProxy
})
proxyOpener = build_opener(proxyHandler)
install_opener(proxyOpener)
fd = urlopen(request, timeout=timeout)
return fd
except socket.timeout as e: