Support for Python 2.5:
- Use minidom to replace ElementTree, which is not available in Python 2.5 - Use urllib2.urlopen to replace urllib.urlopen, so we can get HTTP code - Use cgi.parse_qs when ulrparse.parse_qs is not available
This commit is contained in:
		
							parent
							
								
									3a2260d6af
								
							
						
					
					
						commit
						76cd7d238f
					
				|  | @ -15,11 +15,13 @@ | ||||||
| #    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 | ||||||
|  | @ -59,7 +61,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 +115,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 +155,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 +186,17 @@ 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'): | ||||||
|         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(server.getAttribute('lat')), float(server.getAttribute('lon'))]) | ||||||
|         servers[d] = server.attrib |         servers[d] = dict(server.attributes.items()) | ||||||
| 
 | 
 | ||||||
|     closest = [] |     closest = [] | ||||||
|     for d in sorted(servers.keys())[0:4]: |     for d in sorted(servers.keys())[0:4]: | ||||||
|  | @ -208,11 +217,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 | ||||||
|  | @ -280,14 +289,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