• 机房测速


    想看看欧洲机房到欧洲各地的速度如何,写个脚本,调用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)
    
  • 相关阅读:
    C指针QQ群问答
    英语音标与汉语拼音
    jquery插件validation
    ORACLE纵向表转换为横向表写法
    安装vagrant&virtualBox
    tp5 上传视频到七牛云
    解决VirtualBox错误:“FATAL:No bootable medium found!”
    PyCharm切换Python版本
    七牛云的使用
    一起谈.NET技术,C#中int和System.Int32理解总结 狼人:
  • 原文地址:https://www.cnblogs.com/liujitao79/p/7521440.html
Copyright © 2020-2023  润新知