Send custom User-Agent with all http requests. See #86

This commit is contained in:
Matt Martz 2014-08-20 13:12:30 -05:00
parent 65c85a9b15
commit 60c3ec2a5e
1 changed files with 33 additions and 11 deletions

View File

@ -18,6 +18,7 @@
__version__ = '0.3.1' __version__ = '0.3.1'
# Some global variables we use # Some global variables we use
user_agent = 'speedtest-cli/%s' % __version__
source = None source = None
shutdown_event = None shutdown_event = None
@ -165,6 +166,17 @@ def distance(origin, destination):
return d return d
def build_request(url, data=None, headers={}):
"""Build a urllib2 request object
This function automatically adds a User-Agent header to all requests
"""
headers['User-Agent'] = user_agent
return Request(url, data=data, headers=headers)
class FileGetter(threading.Thread): class FileGetter(threading.Thread):
"""Thread class for retrieving a URL""" """Thread class for retrieving a URL"""
@ -178,7 +190,8 @@ class FileGetter(threading.Thread):
self.result = [0] self.result = [0]
try: try:
if (timeit.default_timer() - self.starttime) <= 10: if (timeit.default_timer() - self.starttime) <= 10:
f = urlopen(self.url) request = build_request(self.url)
f = urlopen(request)
while 1 and not shutdown_event.isSet(): while 1 and not shutdown_event.isSet():
self.result.append(len(f.read(10240))) self.result.append(len(f.read(10240)))
if self.result[-1] == 0: if self.result[-1] == 0:
@ -242,7 +255,8 @@ class FilePutter(threading.Thread):
try: try:
if ((timeit.default_timer() - self.starttime) <= 10 and if ((timeit.default_timer() - self.starttime) <= 10 and
not shutdown_event.isSet()): not shutdown_event.isSet()):
f = urlopen(self.url, self.data) request = build_request(self.url, data=self.data)
f = urlopen(request)
f.read(11) f.read(11)
f.close() f.close()
self.result = len(self.data) self.result = len(self.data)
@ -305,7 +319,8 @@ def getConfig():
we are interested in we are interested in
""" """
uh = urlopen('http://www.speedtest.net/speedtest-config.php') request = build_request('http://www.speedtest.net/speedtest-config.php')
uh = urlopen(request)
configxml = [] configxml = []
while 1: while 1:
configxml.append(uh.read(10240)) configxml.append(uh.read(10240))
@ -342,7 +357,9 @@ def closestServers(client, all=False):
distance distance
""" """
uh = urlopen('http://www.speedtest.net/speedtest-servers-static.php') url = 'http://www.speedtest.net/speedtest-servers-static.php'
request = build_request(url)
uh = urlopen(request)
serversxml = [] serversxml = []
while 1: while 1:
serversxml.append(uh.read(10240)) serversxml.append(uh.read(10240))
@ -408,8 +425,9 @@ def getBestServer(servers):
h = HTTPSConnection(urlparts[1]) h = HTTPSConnection(urlparts[1])
else: else:
h = HTTPConnection(urlparts[1]) h = HTTPConnection(urlparts[1])
headers = {'User-Agent': user_agent}
start = timeit.default_timer() start = timeit.default_timer()
h.request("GET", urlparts[2]) h.request("GET", urlparts[2], headers=headers)
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):
@ -557,7 +575,8 @@ def speedtest():
url = args.mini url = args.mini
urlparts = urlparse(url) urlparts = urlparse(url)
try: try:
f = urlopen(args.mini) request = build_request(args.mini)
f = urlopen(request)
except: except:
print_('Invalid Speedtest Mini URL') print_('Invalid Speedtest Mini URL')
sys.exit(1) sys.exit(1)
@ -568,7 +587,9 @@ def speedtest():
if not extension: if not extension:
for ext in ['php', 'asp', 'aspx', 'jsp']: for ext in ['php', 'asp', 'aspx', 'jsp']:
try: try:
f = urlopen('%s/speedtest/upload.%s' % (args.mini, ext)) request = build_request('%s/speedtest/upload.%s' %
(args.mini, ext))
f = urlopen(request)
except: except:
pass pass
else: else:
@ -663,10 +684,11 @@ def speedtest():
(ping, ulspeedk, dlspeedk, '297aae72')) (ping, ulspeedk, dlspeedk, '297aae72'))
.encode()).hexdigest()] .encode()).hexdigest()]
req = Request('http://www.speedtest.net/api/api.php', headers = {'Referer': 'http://c.speedtest.net/flash/speedtest.swf'}
data='&'.join(apiData).encode()) request = build_request('http://www.speedtest.net/api/api.php',
req.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf') data='&'.join(apiData).encode(),
f = urlopen(req) headers=headers)
f = urlopen(request)
response = f.read() response = f.read()
code = f.code code = f.code
f.close() f.close()