support http(s) proxy
This commit is contained in:
parent
c58ad3367b
commit
b49440786d
21
speedtest.py
21
speedtest.py
|
@ -81,12 +81,12 @@ try:
|
||||||
from urllib2 import (urlopen, Request, HTTPError, URLError,
|
from urllib2 import (urlopen, Request, HTTPError, URLError,
|
||||||
AbstractHTTPHandler, ProxyHandler,
|
AbstractHTTPHandler, ProxyHandler,
|
||||||
HTTPDefaultErrorHandler, HTTPRedirectHandler,
|
HTTPDefaultErrorHandler, HTTPRedirectHandler,
|
||||||
HTTPErrorProcessor, OpenerDirector)
|
HTTPErrorProcessor, OpenerDirector, build_opener as urllib_build_opener)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib.request import (urlopen, Request, HTTPError, URLError,
|
from urllib.request import (urlopen, Request, HTTPError, URLError,
|
||||||
AbstractHTTPHandler, ProxyHandler,
|
AbstractHTTPHandler, ProxyHandler,
|
||||||
HTTPDefaultErrorHandler, HTTPRedirectHandler,
|
HTTPDefaultErrorHandler, HTTPRedirectHandler,
|
||||||
HTTPErrorProcessor, OpenerDirector)
|
HTTPErrorProcessor, OpenerDirector, build_opener as urllib_build_opener)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from httplib import HTTPConnection, BadStatusLine
|
from httplib import HTTPConnection, BadStatusLine
|
||||||
|
@ -571,7 +571,7 @@ class SpeedtestHTTPSHandler(AbstractHTTPHandler):
|
||||||
https_request = AbstractHTTPHandler.do_request_
|
https_request = AbstractHTTPHandler.do_request_
|
||||||
|
|
||||||
|
|
||||||
def build_opener(source_address=None, timeout=10):
|
def build_opener(source_address=None, timeout=10, proxy=None):
|
||||||
"""Function similar to ``urllib2.build_opener`` that will build
|
"""Function similar to ``urllib2.build_opener`` that will build
|
||||||
an ``OpenerDirector`` with the explicit handlers we want,
|
an ``OpenerDirector`` with the explicit handlers we want,
|
||||||
``source_address`` for binding, ``timeout`` and our custom
|
``source_address`` for binding, ``timeout`` and our custom
|
||||||
|
@ -597,8 +597,13 @@ def build_opener(source_address=None, timeout=10):
|
||||||
HTTPRedirectHandler(),
|
HTTPRedirectHandler(),
|
||||||
HTTPErrorProcessor()
|
HTTPErrorProcessor()
|
||||||
]
|
]
|
||||||
|
if proxy:
|
||||||
|
httpproxy_handler = ProxyHandler({"http" : proxy,
|
||||||
|
"https": proxy})
|
||||||
|
opener = urllib_build_opener(httpproxy_handler)
|
||||||
|
else:
|
||||||
|
opener = OpenerDirector()
|
||||||
|
|
||||||
opener = OpenerDirector()
|
|
||||||
opener.addheaders = [('User-agent', build_user_agent())]
|
opener.addheaders = [('User-agent', build_user_agent())]
|
||||||
|
|
||||||
for handler in handlers:
|
for handler in handlers:
|
||||||
|
@ -1074,12 +1079,12 @@ class Speedtest(object):
|
||||||
"""Class for performing standard speedtest.net testing operations"""
|
"""Class for performing standard speedtest.net testing operations"""
|
||||||
|
|
||||||
def __init__(self, config=None, source_address=None, timeout=10,
|
def __init__(self, config=None, source_address=None, timeout=10,
|
||||||
secure=False, shutdown_event=None):
|
secure=False, shutdown_event=None, proxy=None):
|
||||||
self.config = {}
|
self.config = {}
|
||||||
|
|
||||||
self._source_address = source_address
|
self._source_address = source_address
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self._opener = build_opener(source_address, timeout)
|
self._opener = build_opener(source_address, timeout, proxy)
|
||||||
|
|
||||||
self._secure = secure
|
self._secure = secure
|
||||||
|
|
||||||
|
@ -1755,6 +1760,7 @@ def parse_args():
|
||||||
'supplied multiple times')
|
'supplied multiple times')
|
||||||
parser.add_argument('--mini', help='URL of the Speedtest Mini server')
|
parser.add_argument('--mini', help='URL of the Speedtest Mini server')
|
||||||
parser.add_argument('--source', help='Source IP address to bind to')
|
parser.add_argument('--source', help='Source IP address to bind to')
|
||||||
|
parser.add_argument('--proxy', help='use http(s)proxy', default=None)
|
||||||
parser.add_argument('--timeout', default=10, type=PARSER_TYPE_FLOAT,
|
parser.add_argument('--timeout', default=10, type=PARSER_TYPE_FLOAT,
|
||||||
help='HTTP timeout in seconds. Default 10')
|
help='HTTP timeout in seconds. Default 10')
|
||||||
parser.add_argument('--secure', action='store_true',
|
parser.add_argument('--secure', action='store_true',
|
||||||
|
@ -1872,7 +1878,8 @@ def shell():
|
||||||
speedtest = Speedtest(
|
speedtest = Speedtest(
|
||||||
source_address=args.source,
|
source_address=args.source,
|
||||||
timeout=args.timeout,
|
timeout=args.timeout,
|
||||||
secure=args.secure
|
secure=args.secure,
|
||||||
|
proxy=args.proxy
|
||||||
)
|
)
|
||||||
except (ConfigRetrievalError,) + HTTP_ERRORS:
|
except (ConfigRetrievalError,) + HTTP_ERRORS:
|
||||||
printer('Cannot retrieve speedtest configuration', error=True)
|
printer('Cannot retrieve speedtest configuration', error=True)
|
||||||
|
|
Loading…
Reference in New Issue