机房测速

想看看欧洲机房到欧洲各地的速度如何,写个脚本,调用speedtest.net的探针 https://github.com/sivel/speedtest-cli

# -*- coding: utf-8 -*-
# @Author: ken.liu
# @Date:   2017-09-14 11:06:03
# @Last Modified by:   ken.liu
# @Last Modified time: 2017-09-15 10:53:08

import urllib
import re
import subprocess

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

url = 'http://www.speedtest.net/speedtest-servers-static.php'
req = urllib.urlopen(url)
xml = req.read()
fn = 'speedtest.log'

europe = {
    'BE': 'Belgium', 
    'FR': 'France', 
    'BG': 'Bulgaria', 
    'DK': 'Denmark', 
    'HR': 'Croatia', 
    'DE': 'Germany', 
    'HU': 'Hungary', 
    'FI': 'Finland', 
    'YU': 'Yugoslavia', 
    'RU': 'Russia', 
    'NL': 'Netherlands', 
    'UK': 'United Kingdom', 
    'PT': 'Portugal', 
    'NO': 'Norway', 
    'LV': 'Latvia', 
    'LT': 'Lithuania', 
    'RO': 'Romania', 
    'PL': 'Poland', 
    'VA': 'Vatican City', 
    'CH': 'Switzerland', 
    'GR': 'Greece', 
    'EE': 'Estonia', 
    'IS': 'Iceland', 
    'IT': 'Italy', 
    'CZ': 'Czech', 
    'AT': 'Austria', 
    'IE': 'Ireland', 
    'ES': 'Spain', 
    'MD': 'Moldova', 
    'MC': 'Monaco', 
    'SK': 'Slovakia', 
    'MT': 'Malta', 
    'SM': 'San Marino', 
    'UA': 'Ukraine', 
    'SE': 'Sweden', 
    'GB': 'Great Britain'
}

def speedtest(id):
    cmd = ['./speedtest.py --server %s --no-download --csv' % id]
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    if stdout:
        return stdout
    else:
        return stderr

if __name__ == '__main__':
    root = ET.fromstring(xml)
    
    with open(fn, 'w+') as f:
        for server in root.iter('server'):
            if server.attrib['cc'] in europe.keys() and server.attrib['country'] != 'China':
                result = speedtest(server.attrib['id'])
                f.write('%s %s
' % (server.attrib['cc'].encode('utf-8'), server.attrib['country'].encode('utf-8')))
                print result
                f.write(result)
                f.flush()
nohup python parse.py &
# -*- coding: utf-8 -*-
# @Author: ken.liu
# @Date:   2017-09-18 10:49:33
# @Last Modified by:   ken.liu
# @Last Modified time: 2017-09-18 11:20:05


import re

in_fn = 'speedtest.log'
out_fn = 'speedtest_de.csv' 

'''
NL Netherlands
Skipping download test
5794|Digicel Curacao|Willemstad|2017-09-15T14:58:58.979884Z|7356.726340644419|178.768|0|19814022.170529146
'''

partten = re.compile('(^[A-Z]{2}) (S+)\n^Skipping download test\n(^[0-9]+)|(.*)|(.*)|.*|.*|(.*)|0|(.*)\n', re.M) 

def save(cid, country, id, isp, city, ping, download):
    with open(out_fn, 'a') as f:
        f.write('%s|%s|%s|%s|%s|%.2f|%.2f' % (cid, country, city, id, isp, float(ping), float(download)))
        f.write('
')

with open(in_fn, 'r') as f:
    m = partten.findall(f.read())
    if m:
        for i in m:
            cid, country, id, isp, city, ping, download = i
            # ('NL', 'Netherlands', '5794', 'Digicel Curacao', 'Willemstad', '178.768', '19814022.170529146')
            save(cid, country, id, isp, city, ping, download)
原文地址:https://www.cnblogs.com/liujitao79/p/7521440.html