1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import time import requests
# 常见 Docker 镜像源 MIRRORS = { "官方 Docker Hub": "https://registry-1.docker.io", "腾讯云": "https://mirror.ccs.tencentyun.com", "Docker 中国官方": "https://registry.docker-cn.com", "网易": "https://hub-mirror.c.163.com", "百度": "https://mirror.baidubce.com", "中科大": "https://docker.mirrors.ustc.edu.cn", "上海交大": "https://mirror.sjtu.edu.cn", "中科院软件所": "https://mirror.iscas.ac.cn", "阿里云": "https://registry.aliyuncs.com", "dao": "https://docker.m.daocloud.io", "1ms": "https://docker.1ms.run", "xdark": "https://hub.xdark.top", "kubesre": "https://dhub.kubesre.xyz", "kejilion": "https://docker.kejilion.pro", "xuanyuan": "https://docker.xuanyuan.me", "hl": "https://docker.hlmirror.com", "rundocker": "https://run-docker.cn", "sunzi": "https://docker.sunzishaokao.com", "cloudlayer": "https://image.cloudlayer.icu", "docker0": "https://docker-0.unsee.tech", "tbedu": "https://docker.tbedu.top", "crdz": "https://hub.crdz.gq", "meli": "https://docker.melikeme.cn" }
def test_mirror(name, url, timeout=3): """测试单个镜像源是否可用,并返回响应时间(ms),不可用返回 None""" test_url = f"{url}/v2/" try: start = time.time() resp = requests.get(test_url, timeout=timeout) end = time.time() if resp.status_code in (200, 401): # 200 OK 或 401 Unauthorized 都说明源可访问 return round((end - start) * 1000, 2) except requests.RequestException: return None return None
def main(): results = [] print("开始测试 Docker 镜像源可用性...\n") for name, url in MIRRORS.items(): print(f"测试: {name} ({url}) ... ", end="") ms = test_mirror(name, url) if ms is not None: print(f"可用 ✅ 响应时间: {ms} ms") results.append((name, url, ms)) else: print("不可用 ❌")
print("\n====== 测试结果 (按速度排序) ======") if results: results.sort(key=lambda x: x[2]) # 按时间升序 for i, (name, url, ms) in enumerate(results, 1): print(f"{i}. {name:<10} {url:<40} {ms} ms") else: print("没有可用的镜像源。")
if __name__ == "__main__": main()
|