mirror of https://github.com/testerSunshine/12306
use coroutine instead of thread to do cdn test
parent
d0754e4fcd
commit
06f6580527
|
@ -2,37 +2,40 @@
|
||||||
import datetime
|
import datetime
|
||||||
import operator
|
import operator
|
||||||
import os
|
import os
|
||||||
import requests
|
import asyncio
|
||||||
from config import urlConf
|
import functools
|
||||||
import threading
|
|
||||||
from config.urlConf import urls
|
from config.urlConf import urls
|
||||||
|
|
||||||
from myUrllib.httpUtils import HTTPClient
|
from myUrllib.httpUtils import HTTPClient
|
||||||
|
|
||||||
cdn_list = []
|
finish_count = 0
|
||||||
|
|
||||||
|
def run_in_executor(f):
|
||||||
|
@functools.wraps(f)
|
||||||
|
def inner(*args, **kwargs):
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
return loop.run_in_executor(None, lambda: f(*args, **kwargs))
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
class CDNProxy(threading.Thread):
|
def calculate_rtt_blocking(cdn):
|
||||||
def __init__(self, cdns):
|
global finish_count
|
||||||
super().__init__()
|
http = HTTPClient(0)
|
||||||
self.cdns = cdns
|
url = urls["loginInitCdn"]
|
||||||
self.urlConf = urlConf.urls
|
http._cdn = cdn
|
||||||
self.httpClint = requests
|
print(f"测试cdn: {cdn}")
|
||||||
self.city_list = []
|
start_time = datetime.datetime.now()
|
||||||
self.timeout = 5
|
rep = http.send(url)
|
||||||
|
rtt = (datetime.datetime.now() - start_time).microseconds / 1000
|
||||||
|
finish_count += 1
|
||||||
|
print(f"测试完成个数: {finish_count}")
|
||||||
|
return {"ip": cdn, "time": rtt} if rep else None
|
||||||
|
|
||||||
def run(self):
|
|
||||||
for cdn in self.cdns:
|
@run_in_executor
|
||||||
http = HTTPClient(0)
|
def calculate_rtt_async(cdn):
|
||||||
url = urls["loginInitCdn"]
|
return calculate_rtt_blocking(cdn)
|
||||||
http._cdn = cdn.replace("\n", "")
|
|
||||||
start_time = datetime.datetime.now()
|
|
||||||
rep = http.send(url)
|
|
||||||
retTime = (datetime.datetime.now() - start_time).microseconds / 1000
|
|
||||||
if rep and "message" not in rep and retTime < 3000:
|
|
||||||
if cdn.replace("\n", "") not in cdn_list: # 如果有重复的cdn,则放弃加入
|
|
||||||
print(f"加入cdn: {cdn}")
|
|
||||||
cdn_list.append({"ip": cdn.replace("\n", ""), "time": retTime})
|
|
||||||
|
|
||||||
|
|
||||||
def open_cdn_file(cdnFile):
|
def open_cdn_file(cdnFile):
|
||||||
|
@ -52,7 +55,7 @@ def open_cdn_file(cdnFile):
|
||||||
return cdn
|
return cdn
|
||||||
|
|
||||||
|
|
||||||
def sortCdn():
|
def sortCdn(cdn_list):
|
||||||
"""
|
"""
|
||||||
对cdn进行排序
|
对cdn进行排序
|
||||||
:return:
|
:return:
|
||||||
|
@ -65,7 +68,7 @@ def sortCdn():
|
||||||
return ips
|
return ips
|
||||||
|
|
||||||
|
|
||||||
def filterCdn():
|
async def filterCdn():
|
||||||
"""
|
"""
|
||||||
过滤cdn, 过滤逻辑为当前cdn响应值小于1000毫秒
|
过滤cdn, 过滤逻辑为当前cdn响应值小于1000毫秒
|
||||||
过滤日志:
|
过滤日志:
|
||||||
|
@ -73,20 +76,13 @@ def filterCdn():
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
cdns = open_cdn_file("cdn_list")
|
cdns = open_cdn_file("cdn_list")
|
||||||
cdnss = [cdns[i:i + 50] for i in range(0, len(cdns), 50)]
|
cdn_tasks = [calculate_rtt_async(cdn) for cdn in cdns]
|
||||||
cdnThread = []
|
done, pending = await asyncio.wait(fs=cdn_tasks, return_when=asyncio.ALL_COMPLETED)
|
||||||
for cdn in cdnss:
|
cdn_list = [task.result() for task in done if task.exception() is None and task.result() is not None]
|
||||||
t = CDNProxy(cdn)
|
|
||||||
cdnThread.append(t)
|
|
||||||
for cdn_t in cdnThread:
|
|
||||||
cdn_t.start()
|
|
||||||
|
|
||||||
for cdn_j in cdnThread:
|
|
||||||
cdn_j.join()
|
|
||||||
|
|
||||||
print(f"当前有效cdn个数为: {len(cdn_list)}")
|
print(f"当前有效cdn个数为: {len(cdn_list)}")
|
||||||
if cdn_list:
|
if cdn_list:
|
||||||
ips = sortCdn()
|
ips = sortCdn(cdn_list)
|
||||||
path = os.path.join(os.path.dirname(__file__), f'../filter_cdn_list')
|
path = os.path.join(os.path.dirname(__file__), f'../filter_cdn_list')
|
||||||
f = open(path, "a+")
|
f = open(path, "a+")
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import threading
|
||||||
import time
|
import time
|
||||||
import TickerConfig
|
import TickerConfig
|
||||||
import wrapcache
|
import wrapcache
|
||||||
from agency.cdn_utils import CDNProxy, open_cdn_file
|
from agency.cdn_utils import open_cdn_file
|
||||||
from config import urlConf, configCommon
|
from config import urlConf, configCommon
|
||||||
from config.TicketEnmu import ticket
|
from config.TicketEnmu import ticket
|
||||||
from config.configCommon import seat_conf_2, seat_conf
|
from config.configCommon import seat_conf_2, seat_conf
|
||||||
|
|
3
run.py
3
run.py
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding=utf-8 -*-
|
# -*- coding=utf-8 -*-
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
def parser_arguments(argv):
|
def parser_arguments(argv):
|
||||||
|
@ -27,5 +28,5 @@ if __name__ == '__main__':
|
||||||
sendServerChan("订票小助手测试一下")
|
sendServerChan("订票小助手测试一下")
|
||||||
elif args.operate == "c":
|
elif args.operate == "c":
|
||||||
from agency.cdn_utils import filterCdn
|
from agency.cdn_utils import filterCdn
|
||||||
filterCdn()
|
asyncio.run(filterCdn())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue