9-13 29 views
网速通是网宿下的一个全国多节点网络延时测试平台,会统计出丢包率、最大时延、最小时延和平均时延,因为公司业务需要,需要知道全国各地网点到公司数据中心的通信质量,就利用这个平台写了这个简单脚本
声明:只供学习研究,注意并发量和调用间隔,合理利用
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author : Eric Winn # @Email : eng.eric.winn@gmail.com # @Time : 2018/9/13 10:02 # @Version : 1.0 # @File : wangsutong_ping_test.py # @Software : PyCharm import time from datetime import datetime import json import requests import re from multiprocessing import Process nowTime = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') # IP列表 IPS = [ '10.9.5.2', '10.9.5.3', '192.168.1.2', '192.168.1.3' ] class AutoPing(Process): ''' 自定义类,继承了Process类 实例化时需要传入要测试的IP地址 ''' def __init__(self, ip): super().__init__() self.ip = ip self.IPid = '' def url_input(self): ''' 网速通提交IP的Post URL :return: ''' print('[url_input] ====> {}'.format(self.ip)) url = "http://ceba.quansucloud.com/wstCeba/ping/ping-test!test.action?cdnUrl&comparetype=cdn&dnsAdvance=false&dnsServer&urlInput={IPvalue}&urlType=请选择".format( IPvalue=self.ip) urlreq = requests.post(url) req = urlreq.text pattern = re.compile('[a-zA-Z0-9]{32}', re.S) self.IPid = re.search(pattern, req)[0] def get_ping_resut(self): ''' 网速通的测试结果收集接口,会返回所有测试节点的测试状态 :return: ''' print('[get_ping_resut] ====> {}'.format(self.ip)) url = 'http://ceba.quansucloud.com/wstCeba/ping/ping-test!takeTest.action?id={IPid}&count=52'.format( IPid=self.IPid) result = json.loads(requests.get(url).text) if result.get('msg') != 'complete' and result.get('success'): time.sleep(5) self.get_ping_resut() def export_result(self): ''' 获取Excel报表 :return: ''' print('[export_result] ====> {}'.format(self.ip)) url = 'http://ceba.quansucloud.com/wstCeba/ping/ping-test!exportExcel.action?id={IPid}'.format( IPid=self.IPid) header = { "Host": "ceba.quansucloud.com", "Referer": "http://ceba.quansucloud.com/wstCeba/ping/ping-test!result.action?id={IPid}".format( IPid=self.IPid) } result = requests.get(url, headers=header) # 保存到文件 filename = "{}-{}.xls".format(self.ip, nowTime) f = open(filename, "wb") f.write(result.content) f.close() def run(self): self.url_input() time.sleep(15) self.get_ping_resut() self.export_result() if __name__ == '__main__': for ip in IPS: ap = AutoPing(ip) ap.start() |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!