Mercurial > ~darius > hgwebdir.cgi > adslstats
diff speedcheck.py @ 32:1af6865189ce
Update to work with Python 3.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 14 Nov 2020 14:54:05 +1030 |
parents | e73e4677f873 |
children | 815e6b61d76e |
line wrap: on
line diff
--- a/speedcheck.py Sat Nov 14 14:10:50 2020 +1030 +++ b/speedcheck.py Sat Nov 14 14:54:05 2020 +1030 @@ -1,6 +1,6 @@ #!/usr/bin/env python2 -import ConfigParser +import configparser import optparse import os import re @@ -9,7 +9,7 @@ import time def main(): - conf = ConfigParser.ConfigParser() + conf = configparser.ConfigParser() conflist = [] if ('HOME' in os.environ): @@ -37,15 +37,15 @@ parser.error('Graph directory must be specified in either the ini or on the command line') if opts.verbose: - print 'Fetching stats...' + print('Fetching stats...') stats = fetchstats(conf) if opts.verbose: - print stats + print(stats) if opts.verbose: - print 'Updating RRD' + print('Updating RRD') updaterrd(opts.rrd, stats) if opts.verbose: - print 'Updating graph' + print('Updating graph') graphrrd(opts.rrd, opts.graphdir) def fetchstats(conf): @@ -69,7 +69,7 @@ p = subprocess.Popen(['curl', '-w', '%{speed_download}', '-so', '/dev/null', url], stdout = subprocess.PIPE) speed, xxx = p.communicate() if p.returncode != 0: - print 'Error %d fetching \'%s\'' % (p.returncode, url) + print('Error %d fetching \'%s\'' % (p.returncode, url)) return None return float(speed) * 8.0 / 1024.0 # convert to kbit/sec @@ -78,7 +78,7 @@ stdout, stderr = p.communicate() l = stdout.split('\n') if len(l) != 6: - print 'Unable to parse ping line:', l + print('Unable to parse ping line:', l) xx, xx, xx, plossline, latline, xx = l ploss = float(re.match('.* received, ([0-9.]+)% packet loss', plossline).groups()[0]) latency = float(re.match('.*stddev = [0-9.]+/([0-9.]+)/.* ms', latline).groups()[0]) @@ -110,9 +110,9 @@ def updaterrd(rrdname, stats): try: os.stat(rrdname) - except OSError, e: + except OSError as e: if e.errno == 2: - print 'Creating RRD...' + print('Creating RRD...') createrrd(rrdname) s = '%d:' % (int(time.time())) for a in ['neardl', 'nearul', 'nearpl', 'nearlat', 'fardl', 'farul', 'farpl', 'farlat']: