This commit is contained in:
Gal Golan 2016-09-08 19:10:33 +00:00 committed by GitHub
commit 458e056444

View File

@ -24,6 +24,7 @@ import socket
import timeit import timeit
import platform import platform
import threading import threading
import gzip
__version__ = '0.3.4' __version__ = '0.3.4'
@ -48,8 +49,15 @@ except ImportError:
# Begin import game to handle Python 2 and Python 3 # Begin import game to handle Python 2 and Python 3
try: try:
from StringIO import StringIO, BytesIO
except ImportError:
from io import StringIO, BytesIO
try:
import urllib2 as urllib
from urllib2 import urlopen, Request, HTTPError, URLError from urllib2 import urlopen, Request, HTTPError, URLError
except ImportError: except ImportError:
import urllib.request as urllib
from urllib.request import urlopen, Request, HTTPError, URLError from urllib.request import urlopen, Request, HTTPError, URLError
try: try:
@ -375,19 +383,22 @@ def getConfig():
we are interested in we are interested in
""" """
request = build_request('://www.speedtest.net/speedtest-config.php') request = build_request('://www.speedtest.net/speedtest-config.php', headers = {'user-agent':user_agent, 'accept-encoding': 'gzip'})
uh, e = catch_request(request) uh = urllib.build_opener()
if e:
print_('Could not retrieve speedtest.net configuration: %s' % e) response = uh.open(request)
sys.exit(1)
if (response.headers['content-encoding'] == 'gzip'):
text = gzip.GzipFile(fileobj=BytesIO(response.read())).read()
else:
text = response.read()
configxml = [] configxml = []
while 1: configxml.append(text)
configxml.append(uh.read(10240))
if len(configxml[-1]) == 0: if(int(response.code) != 200):
break
if int(uh.code) != 200:
return None return None
uh.close()
try: try:
try: try:
root = ET.fromstring(''.encode().join(configxml)) root = ET.fromstring(''.encode().join(configxml))