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