diff options
| author | 韩丁康 <[email protected]> | 2024-01-09 17:20:16 +0800 |
|---|---|---|
| committer | 韩丁康 <[email protected]> | 2024-01-09 17:20:16 +0800 |
| commit | b97ed22b805c40abc27c689cc307deb364312902 (patch) | |
| tree | 6fe533a96206c11b5bfbccc61eb0a0058458ba3b | |
| parent | 5bf171061f2b1c81c103af091964ac2d16b71dbd (diff) | |
首次同步
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | app.py | 22 | ||||
| -rw-r--r-- | apps/__init__.py | 0 | ||||
| -rw-r--r-- | apps/data.py | 1 | ||||
| -rw-r--r-- | apps/delay.py | 166 | ||||
| -rw-r--r-- | apps/map.py | 8 | ||||
| -rw-r--r-- | apps/statistics.py | 4 | ||||
| -rw-r--r-- | apps/sys.py | 1 |
8 files changed, 206 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2cc8c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +*.out +*.pyc +./app/__pycache__ @@ -0,0 +1,22 @@ +from apiflask import APIFlask,Schema,PaginationSchema +from apiflask.fields import List,Nested +from flask import request +from apps.statistics import bp as statsbp +from apps.delay import bp as delaybp,TestNode +from apps.map import bp as mapbp +import pandas as pd + +# 注册蓝图 +app = APIFlask(__name__,template_folder='./static/templates') +app.register_blueprint(statsbp) +app.register_blueprint(delaybp) +app.register_blueprint(mapbp) + [email protected]('/') [email protected]("获取测试页面") +def hello(): + return "这是一个正常的测试页面" + + +if __name__ == '__main__': + app.run(host="0.0.0.0",debug=True,port=8484) diff --git a/apps/__init__.py b/apps/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/apps/__init__.py diff --git a/apps/data.py b/apps/data.py new file mode 100644 index 0000000..f7c6ef6 --- /dev/null +++ b/apps/data.py @@ -0,0 +1 @@ +# 负责数据检索查询
\ No newline at end of file diff --git a/apps/delay.py b/apps/delay.py new file mode 100644 index 0000000..61be9dc --- /dev/null +++ b/apps/delay.py @@ -0,0 +1,166 @@ +# 时延测试接口 +import random +import threading + +import requests +from apiflask.validators import OneOf +from apiflask import APIFlask,APIBlueprint,Schema +from apiflask.fields import Integer,String,List,Nested,IP,Dict +from flask import request +import pandas as pd +from requests.exceptions import Timeout +import numpy as np + +bp=APIBlueprint("delay",__name__,url_prefix="/delay") + +icmp_delaytable={} +tcp_delaytable={} +dns_delaytable={} + +class TestNode(Schema): + Id = Integer() + Name=String() + Ip=String() + Loc=String() + Port=Integer() + +class Delay(Schema): + Id=Integer() + CurrDelay=String() + # MeanDelay=Integer() + # MaxDelay=Integer() + Type=String() +class DelayOut(Schema): + delay_data=List(Nested(Delay)) + +# 时延数据列表初始化 +# icmpdelay_state={} +# tcpdelay_state={} +# dnsdelay_state={} +# df=pd.read_csv("./server.csv",encoding="utf-8") +# for index,row in df.iterrows(): +# icmpdelay_state[row['id']]={} +# tcpdelay_state[row['id']]={} +# dnsdelay_state[row['id']]={} + + [email protected]("/<string:type>") [email protected]("获取每个节点的时延数据","type参数为{icmp,dns,tcp}中的一个") [email protected]({"ip":IP(required=False)},location="query") [email protected](DelayOut) +def get_pernode_delay(query_data,type): + addr="" + if 'ip' in query_data.keys(): + addr=query_data['ip'] + ans = [] + threads=[] + df = pd.read_csv("./server.csv", encoding="utf-8") + for index, row in df.iterrows(): + mythread=threading.Thread(target=task,args=[ans,addr,row,type]) + mythread.start() + threads.append(mythread) + for t in threads: + t.join() + # if type=="icmp": + # df=pd.read_csv("./server.csv",encoding="utf-8") + # for index,row in df.iterrows(): + # ans.append({ + # 'Id':row['id'], + # 'CurrDelay':icmp_delay_query(addr,row['ip']), + # 'Type':type}) + # if type=="tcp": + # df=pd.read_csv("./server.csv",encoding="utf-8") + # for index,row in df.iterrows(): + # ans.append({ + # 'Id':row['id'], + # 'CurrDelay':tcp_delay_query(addr,row['ip']), + # 'Type':type}) + # if type=="dns": + # df=pd.read_csv("./server.csv",encoding="utf-8") + # for index,row in df.iterrows(): + # ans.append({ + # 'Id':row['id'], + # 'CurrDelay':dns_delay_query(addr,row['ip']), + # 'Type':type}) + return {'delay_data':ans} + +threadLock = threading.Lock() +def task(ans,addr,row,type): + if type=="icmp": + res=icmp_delay_query(addr,row['ip']) + threadLock.acquire() + ans.append({ + 'Id':row['id'], + 'CurrDelay':res, + 'Type':type}) + threadLock.release() + return + if type=="tcp": + res=tcp_delay_query(addr, row['ip']) + threadLock.acquire() + ans.append({ + 'Id':row['id'], + 'CurrDelay':res, + 'Type':type}) + threadLock.release() + return + if type=="dns": + res=dns_delay_query(addr, row['ip']) + threadLock.acquire() + ans.append({ + 'Id':row['id'], + 'CurrDelay':res, + 'Type':type}) + threadLock.release() + +def icmp_delay_query(target,addr): + try: + res=requests.get(url="http://"+addr+":2525/script/icmpdelay",params={'ip':target},timeout=5) + print("icmp ok:" + addr + "-------" + res.text+"-------"+str(res.elapsed.total_seconds())) + icmp_delaytable[str(addr)+str(target)]=res.text + return res.text + except Timeout: + # 如果存在旧数据 + if str(addr)+str(target) in icmp_delaytable.keys(): + pass + # 不存在则设0 + else: + icmp_delaytable[str(addr)+str(target)]=0 + return icmp_delaytable[str(addr)+str(target)] + + + +def tcp_delay_query(target,addr): + try: + res = requests.get(url="http://" + addr + ":2525/script/tcpdelay", params={'ip': target, 'port': 53}, timeout=5) + print("tcp ok:" + addr + "-------" + res.text) + tcp_delaytable[str(addr)+str(target)] = res.text + return res.text + except Timeout: + # 如果存在旧数据 + if str(addr) + str(target) in tcp_delaytable.keys(): + pass + # 不存在则设0 + else: + tcp_delaytable[str(addr)+str(target)] = 0 + return tcp_delaytable[str(addr)+str(target)] + + + +def dns_delay_query(target,addr): + try: + res = requests.get(url="http://" + addr + ":2525/script/dnsdelay", params={'ip': target},timeout=5) + print("dns ok:" + addr + "-------" + res.text) + dns_delaytable[str(addr)+str(target)] = res.text + return dns_delaytable[str(addr)+str(target)] + except Timeout: + # 如果存在旧数据 + if str(addr) + str(target) in dns_delaytable.keys(): + pass + # 不存在则设0 + else: + dns_delaytable[str(addr)+str(target)] = 0 + return dns_delaytable[str(addr)+str(target)] + + + diff --git a/apps/map.py b/apps/map.py new file mode 100644 index 0000000..78bb8a2 --- /dev/null +++ b/apps/map.py @@ -0,0 +1,8 @@ +from apiflask import APIFlask,APIBlueprint +from flask import render_template + +bp=APIBlueprint("map",__name__,url_prefix="/map") + [email protected]("/") +def showmap(): + return render_template("/index.html")
\ No newline at end of file diff --git a/apps/statistics.py b/apps/statistics.py new file mode 100644 index 0000000..3a9c521 --- /dev/null +++ b/apps/statistics.py @@ -0,0 +1,4 @@ +# 展示统计信息 +from apiflask import APIFlask,APIBlueprint,Schema + +bp=APIBlueprint("stats",__name__,url_prefix="/stats")
\ No newline at end of file diff --git a/apps/sys.py b/apps/sys.py new file mode 100644 index 0000000..0027566 --- /dev/null +++ b/apps/sys.py @@ -0,0 +1 @@ +# 处理系统信息,包括状态,已运行时间
\ No newline at end of file |
