-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (93 loc) · 2.7 KB
/
main.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import requests
import json
import eel
import socket
import speedtest
import datetime
import geocoder
import folium
import threading
import time
from collections import deque
import psutil
def calc_ul_dl(rate, dt=3, interface="Беспроводная сеть"):
t0 = time.time()
counter = psutil.net_io_counters(pernic=True)[interface]
tot = (counter.bytes_sent, counter.bytes_recv)
while True:
last_tot = tot
time.sleep(dt)
counter = psutil.net_io_counters(pernic=True)[interface]
t1 = time.time()
tot = (counter.bytes_sent, counter.bytes_recv)
ul, dl = [
(now - last) / (t1 - t0) / 1024.0
for now, last in zip(tot, last_tot)
]
rate.append((ul, dl))
t0 = time.time()
def print_rate(rate):
try:
return "Upload Speed: {0:.0f} kB/s / Download Speed: {1:.0f} kB/s".format(*rate[-1])
except IndexError:
return "UL: - kB/s/ DL: - kB/s"
def return_rate(rate):
try:
return rate[-1]
except IndexError:
return [0, 0]
def connections():
cons = [connection[5] for connection in (psutil.net_connections())]
nons = cons.count('NONE')
establisheds = cons.count('ESTABLISHED')
listens = cons.count('LISTEN')
time_waits = cons.count('TIME_WAIT')
traffic = {
'None': 'None: ' + str(nons),
'Established': 'Established: ' + str(establisheds),
'Listen': 'Listen: ' + str(listens),
'Time_Wait': 'Time_Wait: ' + str(time_waits)
}
return traffic
def get_map():
ip = geocoder.ip("me")
location = ip.latlng
m = folium.Map(location=location, zoom_start=10)
folium.Marker(location).add_to(m)
m.save("Web/map.html")
return ip
info = []
def get_info(transferrate):
dt = datetime.datetime.now()
data = {}
# data["ip"] = socket.gethostbyname("me")
data["ip"] = '0'
data['i'] = dt.second
data["proxy"] = ''
data["speed"] = print_rate(transferrate)
u, d = return_rate(transfer_rate)
if len(info) > 60:
info.pop(0)
info.append({
"tm": dt.hour * 3600 + dt.minute * 60 + dt.second,
"up": float('{:.1f}'.format(u)),
"dn": float('{:.1f}'.format(d))
})
data["information"] = info
data["time"] = f'{dt.hour}:{dt.minute}:{dt.second}'
data["traffic"] = connections()
with open('Web/info.json', 'r+') as f:
json.dump(data, f)
f.truncate()
get_map()
# eel.init('Web')
# eel.start('main.html', size=(800, 500))
transfer_rate = deque(maxlen=1)
t = threading.Thread(target=calc_ul_dl, args=(transfer_rate,))
t.daemon = True
t.start()
run = True
while run:
print(return_rate(transfer_rate))
get_info(transfer_rate)
time.sleep(1)