Fixed Gzip in Python 3

This commit is contained in:
galgolan 2016-09-08 22:09:19 +03:00
parent 76264fd19f
commit ff76b18f87
1 changed files with 10 additions and 5 deletions

View File

@ -24,8 +24,6 @@ import socket
import timeit import timeit
import platform import platform
import threading import threading
import urllib2
import StringIO
import gzip import gzip
__version__ = '0.3.4' __version__ = '0.3.4'
@ -51,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:
@ -379,12 +384,12 @@ def getConfig():
""" """
request = build_request('://www.speedtest.net/speedtest-config.php', headers = {'user-agent':user_agent, 'accept-encoding': 'gzip'}) request = build_request('://www.speedtest.net/speedtest-config.php', headers = {'user-agent':user_agent, 'accept-encoding': 'gzip'})
uh = urllib2.build_opener() uh = urllib.build_opener()
response = uh.open(request) response = uh.open(request)
if (response.headers['content-encoding'] == 'gzip'): if (response.headers['content-encoding'] == 'gzip'):
text = gzip.GzipFile(fileobj=StringIO.StringIO(response.read())).read() text = gzip.GzipFile(fileobj=BytesIO(response.read())).read()
else: else:
text = response.read() text = response.read()