想看看欧洲机房到欧洲各地的速度如何,写个脚本,调用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\n' % (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('\n')
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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类