Use custom user agent
This commit is contained in:
parent
71b456594f
commit
1f16fb3cfe
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
__version__ = '1.0.0'
|
__version__ = '1.0.0'
|
||||||
|
|
||||||
|
USER_AGENT = 'speedtest-cli/%s' % __version__
|
||||||
|
|
||||||
class FakeShutdownEvent(object):
|
class FakeShutdownEvent(object):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -200,7 +201,9 @@ class Downloader(threading.Thread):
|
||||||
self.result = [0]
|
self.result = [0]
|
||||||
try:
|
try:
|
||||||
if (time.time() - self.starttime) <= 10:
|
if (time.time() - self.starttime) <= 10:
|
||||||
f = urlopen(self.url)
|
req = Request(self.url)
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
|
f = urlopen(req)
|
||||||
while (1 and not shutdown_event.isSet() and
|
while (1 and not shutdown_event.isSet() and
|
||||||
(time.time() - self.starttime) <= 10):
|
(time.time() - self.starttime) <= 10):
|
||||||
self.result.append(len(f.read(10240)))
|
self.result.append(len(f.read(10240)))
|
||||||
|
@ -228,7 +231,9 @@ class Uploader(threading.Thread):
|
||||||
try:
|
try:
|
||||||
if ((time.time() - self.starttime) <= 10 and
|
if ((time.time() - self.starttime) <= 10 and
|
||||||
not shutdown_event.isSet()):
|
not shutdown_event.isSet()):
|
||||||
f = urlopen(self.url, self.data)
|
req = Request(self.url, self.data)
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
|
f = urlopen(req)
|
||||||
f.read(11)
|
f.read(11)
|
||||||
f.close()
|
f.close()
|
||||||
self.result = len(self.data)
|
self.result = len(self.data)
|
||||||
|
@ -393,6 +398,7 @@ class SpeedtestResults(object):
|
||||||
|
|
||||||
req = Request('http://www.speedtest.net/api/api.php',
|
req = Request('http://www.speedtest.net/api/api.php',
|
||||||
data='&'.join(api_data).encode())
|
data='&'.join(api_data).encode())
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
req.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf')
|
req.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf')
|
||||||
try:
|
try:
|
||||||
f = urlopen(req)
|
f = urlopen(req)
|
||||||
|
@ -451,19 +457,21 @@ class Speedtest(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
uh = urlopen('http://www.speedtest.net/speedtest-config.php')
|
req = Request('http://www.speedtest.net/speedtest-config.php')
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
|
f = urlopen(req)
|
||||||
except (URLError, HTTPError):
|
except (URLError, HTTPError):
|
||||||
raise ConfigRetrievalError
|
raise ConfigRetrievalError
|
||||||
|
|
||||||
configxml = []
|
configxml = []
|
||||||
while 1:
|
while 1:
|
||||||
configxml.append(uh.read(10240))
|
configxml.append(f.read(10240))
|
||||||
if len(configxml[-1]) == 0:
|
if len(configxml[-1]) == 0:
|
||||||
break
|
break
|
||||||
if int(uh.code) != 200:
|
if int(f.code) != 200:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
uh.close()
|
f.close()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
root = ET.fromstring(''.encode().join(configxml))
|
root = ET.fromstring(''.encode().join(configxml))
|
||||||
|
@ -526,19 +534,21 @@ class Speedtest(object):
|
||||||
'be int' % s)
|
'be int' % s)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
uh = urlopen('http://www.speedtest.net/speedtest-servers.php')
|
req = Request('http://www.speedtest.net/speedtest-servers.php')
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
|
f = urlopen(req)
|
||||||
except (URLError, HTTPError):
|
except (URLError, HTTPError):
|
||||||
raise ServersRetrievalError
|
raise ServersRetrievalError
|
||||||
|
|
||||||
serversxml = []
|
serversxml = []
|
||||||
while 1:
|
while 1:
|
||||||
serversxml.append(uh.read(10240))
|
serversxml.append(f.read(10240))
|
||||||
if len(serversxml[-1]) == 0:
|
if len(serversxml[-1]) == 0:
|
||||||
break
|
break
|
||||||
if int(uh.code) != 200:
|
if int(f.code) != 200:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
uh.close()
|
f.close()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
root = ET.fromstring(''.encode().join(serversxml))
|
root = ET.fromstring(''.encode().join(serversxml))
|
||||||
|
@ -596,7 +606,9 @@ class Speedtest(object):
|
||||||
urlparts = urlparse(url)
|
urlparts = urlparse(url)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = urlopen(server)
|
req = Request(server)
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
|
f = urlopen(req)
|
||||||
except (URLError, HTTPError):
|
except (URLError, HTTPError):
|
||||||
raise SpeedtestMiniConnectFailure('Failed to connect to %s' %
|
raise SpeedtestMiniConnectFailure('Failed to connect to %s' %
|
||||||
server)
|
server)
|
||||||
|
@ -655,18 +667,20 @@ class Speedtest(object):
|
||||||
url = os.path.dirname(server['url'])
|
url = os.path.dirname(server['url'])
|
||||||
for _ in range(0, 3):
|
for _ in range(0, 3):
|
||||||
try:
|
try:
|
||||||
uh = urlopen('%s/latency.txt' % url)
|
req = Request('%s/latency.txt' % url)
|
||||||
|
req.add_header('User-Agent', USER_AGENT)
|
||||||
|
f = urlopen(req)
|
||||||
except (HTTPError, URLError):
|
except (HTTPError, URLError):
|
||||||
cum.append(3600)
|
cum.append(3600)
|
||||||
continue
|
continue
|
||||||
start = time.time()
|
start = time.time()
|
||||||
text = uh.read(9)
|
text = f.read(9)
|
||||||
total = time.time() - start
|
total = time.time() - start
|
||||||
if int(uh.code) == 200 and text == 'test=test'.encode():
|
if int(f.code) == 200 and text == 'test=test'.encode():
|
||||||
cum.append(total)
|
cum.append(total)
|
||||||
else:
|
else:
|
||||||
cum.append(3600)
|
cum.append(3600)
|
||||||
uh.close()
|
f.close()
|
||||||
avg = round((sum(cum) / 3) * 1000000, 3)
|
avg = round((sum(cum) / 3) * 1000000, 3)
|
||||||
results[avg] = server
|
results[avg] = server
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue