前几天因用过某大佬的谷歌翻译修复工具,感觉挺有意思。所以用python重写了一个类似版本,此工具支持使用配置文件测试任何网站各个IP的延迟,以此取最优IP,达到修复网站卡顿或者打不开的效果。
工具原理是基于PING测试,代码在下面开源。(写的有点乱,请自动无视)
附带两个写好的配置,github和谷歌翻译的。有自己写好的也可以发给我,个人精力有限。
源码:
[Python] 纯文本查看 复制代码 import json
import logging
import os
import time
from ping3 import ping
def Set_json(dict, fname):
"""
写出字典数据到文件
:param dict: 字典变量
:param fname: 写出到的文件名
:return:
"""
f = open(fname, 'w', encoding='utf-8')
f.write(json.dumps(dict, ensure_ascii=False, indent=4))
f.close()
return True
def Get_json(fname):
"""
读取字典文件
:param fname: 字典文件(json)
:return: 读取到的字典数据
"""
f = open(fname, 'r', encoding='utf-8')
js = json.loads(f.read())
f.close()
return js
class PingCs:
host = ''
ip_list = []
out_dict = {}
jsonip_dcit = {}
return_dict={}
def __init__(self):
logging.basicConfig(level=logging.DEBUG)
def read_iptxt(self, txt_pwd):
"""
读取txt简易版配置文件,第一行域名,第2行及以后IP
:param txt_pwd:
:return:
"""
txt_list = open(txt_pwd, 'r').read().split('\n')
self.jsonip_dcit = {txt_pwd: {"host": txt_list[0], "ip_list": txt_list[1:]}}
def read_ipjson(self, jsonpwd):
"""
读取json配置文件,格式为{'name':{host:域名,ip_list:['ip','ip2'....]}}
:param jsonpwd:
:return:
"""
self.jsonip_dcit = Get_json(jsonpwd)
def run_pings(self, ip_list=None):
"""
ping ip列表,写出到out_dict
:param ip_list: ['ip','ip2'....]
:return:
"""
if ip_list is None:
ip_list = self.ip_list
out_dict = {}
for i in ip_list:
out_dict[i] = self.ping_host(i)
logging.info(f'ip:{i} 延迟:{out_dict[i]}')
self.out_dict = out_dict
def ping_host(self, ip_address):
"""
ping ip地址
:param ip_address: str ip地址
:return:
"""
response = ping(ip_address)
if response is not None:
delay = int(response * 1000)
return delay
else:
return 9999
def min_ping(self, data_dict=None):
"""
取out_dict的最小值
:param data_dict:
:return:
"""
if data_dict is None:
data_dict = self.out_dict
min_key = None
min_value = 99999
for i in data_dict.keys():
if data_dict[i] < min_value:
min_key = i
min_value = data_dict[i]
logging.info(f"最优ip为{min_key},延迟{min_value}秒")
time.sleep(0.1)
return min_key
def dctostr(self, host, ip):
return f'{host} {ip}'
def writehost(self, hoststr):
pass
def run(self):
files = os.listdir('./')
for fileid in range(len(files) - 1, -1, -1):
if '.txt' not in files[fileid] and '.json' not in files[fileid]:
files.remove(files[fileid])
print('请输入序号,选择要加载的配置文件。')
for id in range(len(files)):
print(f'{id} : {files[id]}')
fileid = int(input('请输入序号:'))
logging.info(f'开始测试配置:{files[fileid]}\n')
if '.txt' in files[fileid]:
self.read_iptxt(files[fileid])
if '.json' in files[fileid]:
self.read_ipjson(files[fileid])
# print(json.dumps(self.jsonip_dcit) )
for key in self.jsonip_dcit.values():
print(f'测试网址:{key["host"]}')
self.host=key['host']
self.ip_list=key['ip_list']
self.run_pings()
time.sleep(0.2)
min_ip=self.min_ping()
logging.info('\n\n')
self.return_dict[self.host]=min_ip
print('\n测速完成,请将以下内容写入host文件尾部\n\n')
time.sleep(0.5)
for hsot in self.return_dict.keys():
print(f'{self.return_dict[hsot]} {hsot}')
input("\n\n按任意键结束程序。。。")
PingCs().run()
运行截图:
有条件的可以直接用源码,或者自行编译。不想费劲的可以下载编译好的。
下载地址:https://wwtc.lanzoue.com/b012dk74d密码:3orm
解压密码:52pojie |