support HTTP and HTTPS proxy from env
This commit is contained in:
parent
795bc51da4
commit
a7218e3fb6
|
@ -48,6 +48,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib.request import urlopen, Request, HTTPError, URLError
|
from urllib.request import urlopen, Request, HTTPError, URLError
|
||||||
|
|
||||||
|
try:
|
||||||
|
from urllib2 import getproxies
|
||||||
|
except ImportError:
|
||||||
|
from urllib.request import getproxies
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from httplib import HTTPConnection, HTTPSConnection
|
from httplib import HTTPConnection, HTTPSConnection
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -404,12 +409,25 @@ def getBestServer(servers):
|
||||||
urlparts = urlparse(url)
|
urlparts = urlparse(url)
|
||||||
for i in range(0, 3):
|
for i in range(0, 3):
|
||||||
try:
|
try:
|
||||||
if urlparts[0] == 'https':
|
proxy_set = getproxies()
|
||||||
|
if proxy_set:
|
||||||
|
if 'http' in proxy_set:
|
||||||
|
proxy_path = urlparse(proxy_set['http'])
|
||||||
|
h = HTTPConnection(proxy_path[1] or proxy_path[2])
|
||||||
|
elif 'https' in proxy_set:
|
||||||
|
proxy_path = urlparse(proxy_set['https'])
|
||||||
|
h = HTTPSConnection(proxy_path[1] or proxy_path[2])
|
||||||
|
request_url = proxy_path.scheme + "://" + urlparts[1:3]
|
||||||
|
|
||||||
|
elif urlparts[0] == 'https':
|
||||||
h = HTTPSConnection(urlparts[1])
|
h = HTTPSConnection(urlparts[1])
|
||||||
|
request_url = urlparts[2]
|
||||||
else:
|
else:
|
||||||
h = HTTPConnection(urlparts[1])
|
h = HTTPConnection(urlparts[1])
|
||||||
|
request_url = urlparts[2]
|
||||||
|
|
||||||
start = timeit.default_timer()
|
start = timeit.default_timer()
|
||||||
h.request("GET", urlparts[2])
|
h.request("GET", request_url)
|
||||||
r = h.getresponse()
|
r = h.getresponse()
|
||||||
total = (timeit.default_timer() - start)
|
total = (timeit.default_timer() - start)
|
||||||
except (HTTPError, URLError, socket.error):
|
except (HTTPError, URLError, socket.error):
|
||||||
|
|
Loading…
Reference in New Issue