Merge pull request #6 from bcse/legacy-support
Add support for Python 2.4 and 2.5
This commit is contained in:
commit
9e1a582826
|
@ -15,15 +15,20 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import urllib
|
||||
import urllib2
|
||||
import urlparse
|
||||
try:
|
||||
from urlparse import parse_qs
|
||||
except ImportError:
|
||||
from cgi import parse_qs
|
||||
import math
|
||||
import xml.etree.ElementTree as ET
|
||||
from xml.dom import minidom as DOM
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
import hashlib
|
||||
try:
|
||||
from hashlib import md5
|
||||
except:
|
||||
from md5 import md5
|
||||
import threading
|
||||
from Queue import Queue
|
||||
|
||||
|
@ -59,7 +64,7 @@ class FileGetter(threading.Thread):
|
|||
def run(self):
|
||||
try:
|
||||
if (time.time() - self.starttime) <= 10:
|
||||
f = urllib.urlopen(self.url)
|
||||
f = urllib2.urlopen(self.url)
|
||||
contents = f.read()
|
||||
f.close()
|
||||
self.result = contents
|
||||
|
@ -113,7 +118,7 @@ class FilePutter(threading.Thread):
|
|||
def run(self):
|
||||
try:
|
||||
if (time.time() - self.starttime) <= 10:
|
||||
f = urllib.urlopen(self.url, self.data)
|
||||
f = urllib2.urlopen(self.url, self.data)
|
||||
contents = f.read()
|
||||
f.close()
|
||||
self.result = self.data
|
||||
|
@ -153,22 +158,29 @@ def uploadSpeed(url, sizes):
|
|||
return (len(''.join(finished))/(time.time()-start))
|
||||
|
||||
|
||||
def getAttributesByTagName(dom, tagName):
|
||||
elem = dom.getElementsByTagName(tagName)[0]
|
||||
return dict(elem.attributes.items())
|
||||
|
||||
|
||||
def getConfig():
|
||||
"""Download the speedtest.net configuration and return only the data
|
||||
we are interested in
|
||||
"""
|
||||
|
||||
uh = urllib.urlopen('http://www.speedtest.net/speedtest-config.php')
|
||||
uh = urllib2.urlopen('http://www.speedtest.net/speedtest-config.php')
|
||||
configxml = uh.read()
|
||||
if int(uh.getcode()) != 200:
|
||||
if int(uh.code) != 200:
|
||||
return None
|
||||
uh.close()
|
||||
root = ET.fromstring(configxml)
|
||||
root = DOM.parseString(configxml)
|
||||
config = {
|
||||
'client': root.find('client').attrib,
|
||||
'times': root.find('times').attrib,
|
||||
'download': root.find('download').attrib,
|
||||
'upload': root.find('upload').attrib}
|
||||
'client': getAttributesByTagName(root, 'client'),
|
||||
'times': getAttributesByTagName(root, 'times'),
|
||||
'download': getAttributesByTagName(root, 'download'),
|
||||
'upload': getAttributesByTagName(root, 'upload')}
|
||||
|
||||
del root
|
||||
return config
|
||||
|
||||
|
||||
|
@ -177,17 +189,18 @@ def closestServers(client):
|
|||
distance
|
||||
"""
|
||||
|
||||
uh = urllib.urlopen('http://speedtest.net/speedtest-servers.php')
|
||||
uh = urllib2.urlopen('http://speedtest.net/speedtest-servers.php')
|
||||
serversxml = uh.read()
|
||||
if int(uh.getcode()) != 200:
|
||||
if int(uh.code) != 200:
|
||||
return None
|
||||
uh.close()
|
||||
root = ET.fromstring(serversxml)
|
||||
root = DOM.parseString(serversxml)
|
||||
servers = {}
|
||||
for server in root[0]:
|
||||
for server in root.getElementsByTagName('server'):
|
||||
attrib = dict(server.attributes.items())
|
||||
d = distance([float(client['lat']), float(client['lon'])],
|
||||
[float(server.get('lat')), float(server.get('lon'))])
|
||||
servers[d] = server.attrib
|
||||
[float(attrib.get('lat')), float(attrib.get('lon'))])
|
||||
servers[d] = attrib
|
||||
|
||||
closest = []
|
||||
for d in sorted(servers.keys())[0:4]:
|
||||
|
@ -208,11 +221,11 @@ def getBestServer(servers):
|
|||
cum = 0
|
||||
url = os.path.dirname(server['url'])
|
||||
for i in xrange(0, 3):
|
||||
uh = urllib.urlopen('%s/latency.txt' % url)
|
||||
uh = urllib2.urlopen('%s/latency.txt' % url)
|
||||
start = time.time()
|
||||
text = uh.read().strip()
|
||||
total = time.time() - start
|
||||
if int(uh.getcode()) == 200 and text == 'test=test':
|
||||
if int(uh.code) == 200 and text == 'test=test':
|
||||
cum += total
|
||||
else:
|
||||
cum += 3600
|
||||
|
@ -271,7 +284,7 @@ def speedtest():
|
|||
'recommendedserverid=%s' % best['id'],
|
||||
'accuracy=%s' % 1,
|
||||
'serverid=%s' % best['id'],
|
||||
'hash=%s' % hashlib.md5('%s-%s-%s-%s' %
|
||||
'hash=%s' % md5('%s-%s-%s-%s' %
|
||||
(ping, ulspeedk, dlspeedk, '297aae72')
|
||||
).hexdigest()]
|
||||
|
||||
|
@ -280,14 +293,14 @@ def speedtest():
|
|||
req.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf')
|
||||
f = urllib2.urlopen(req)
|
||||
response = f.read()
|
||||
code = f.getcode()
|
||||
code = f.code
|
||||
f.close()
|
||||
|
||||
if int(code) != 200:
|
||||
print 'Could not submit results to speedtest.net'
|
||||
sys.exit(1)
|
||||
|
||||
qsargs = urlparse.parse_qs(response)
|
||||
qsargs = parse_qs(response)
|
||||
resultid = qsargs.get('resultid')
|
||||
if not resultid or len(resultid) != 1:
|
||||
print 'Could not submit results to speedtest.net'
|
||||
|
|
Loading…
Reference in New Issue