-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingTester.py
387 lines (326 loc) · 14 KB
/
PingTester.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from os import path
import tkinter as tk
import sys
import os
from tkinter import ttk
import customtkinter as ctk
from subprocess import run, PIPE, CREATE_NO_WINDOW
from time import sleep
from threading import Thread
import chardet
from configparser import ConfigParser
from pystray import Menu, MenuItem, Icon
from PIL.Image import open as open_image
from PIL import ImageTk
import re
from CTkMessagebox import CTkMessagebox
import json
import winreg as reg
import webbrowser
from os import path, getcwd
import io
def set_value_in_file(key, value):
config_file = "settings.json"
config = {}
if path.exists(config_file):
with open(config_file, "r") as file:
try:
config = json.load(file)
except json.JSONDecodeError:
pass # If there's an error, just overwrite with new settings
config[key] = value
with open(config_file, "w") as file:
json.dump(config, file)
def create_or_open_key():
registry_path = r'Software\PingTester' # Fixed path for the registry key
try:
# Try to open the key if it already exists
registry_key = reg.OpenKey(reg.HKEY_CURRENT_USER, registry_path, 0, reg.KEY_WRITE)
except FileNotFoundError:
# If the key does not exist, create it
registry_key = reg.CreateKey(reg.HKEY_CURRENT_USER, registry_path)
return registry_key
def get_reg_value(key, default):
registry_path = r'Software\PingTester' # Use a more unique path to avoid conflicts
try:
with reg.OpenKey(reg.HKEY_CURRENT_USER, registry_path, 0, reg.KEY_READ) as registry_key:
value, _ = reg.QueryValueEx(registry_key, key)
return value
except OSError as e:
print(f"OSError while reading from registry: {e}")
print("Falling back on default settings")
# Fallback to a local file if registry access fails
return get_value_from_file(key, default)
def get_value_from_file(key, default):
config_file = "settings.json" # Local configuration file
if path.exists(config_file):
with open(config_file, "r") as file:
try:
config = json.load(file)
return config.get(key, default)
except json.JSONDecodeError as e:
print(f"Error reading from config file: {e}")
return default
def set_reg_value(key, value):
try:
with create_or_open_key() as registry_key:
reg.SetValueEx(registry_key, key, 0, reg.REG_SZ, json.dumps(value))
except WindowsError:
pass
def safe_update(callable, *args):
if 'settings_window_instance' in globals() and settings_window_instance is not None and settings_window_instance.winfo_exists():
settings_window_instance.after(0, callable, *args)
def apply_changes(entries):
def apply_changes_inner():
config_to_save = {}
for key in entries:
config_to_save[key] = entries[key].get()
try:
set_reg_value('Settings', config_to_save)
print("Changes applied to registry")
global settings_changed
settings_changed = True
except OSError as e:
print(f"Failed to write to registry: {e}, saving to file instead.")
for key, value in config_to_save.items():
set_value_in_file(key, value)
print("Changes applied to file")
safe_update(apply_changes_inner)
def update_tray_tooltip_safe(tooltip):
if 'tray_icon' in globals() and tray_icon is not None:
tray_icon.title = tooltip
def icon_change_safe(name):
if 'tray_icon' in globals() and tray_icon is not None:
icon_change(name)
def open_settings_window():
global settings_window_instance
if settings_window_instance is not None:
settings_window_instance.deiconify()
settings_window_instance.lift()
return
ctk.set_appearance_mode("Dark")
settings_window_instance = ctk.CTk()
settings_window_instance.title("Settings")
tabs = ctk.CTkTabview(settings_window_instance)
tabs.pack(fill="both", padx=10, pady=(0,11))
icon_path = get_icon_path('Perfect.ico')
settings_window_instance.after(201, lambda: settings_window_instance.iconbitmap(icon_path))
config_tab = tabs.add("Configuration")
entries = {}
default_config = {
"ping_count": 10,
"seconds_between_pings": 0.33,
"max_response_time": 150,
"server_to_ping": "google.com",
"perfect_threshold": 9,
"good_threshold": 18,
"medium_threshold": 28
}
try:
stored_config_str = get_reg_value('Settings', '{}')
stored_config = json.loads(stored_config_str) if stored_config_str else default_config
except json.JSONDecodeError:
print("Error loading configuration from registry. Using default configuration.")
stored_config = default_config
for i, (key, default) in enumerate(default_config.items()):
label = ctk.CTkLabel(config_tab, text=key.replace('_', ' ').capitalize() + ":")
label.grid(row=i, column=0, padx=10, pady=5, sticky="w")
value = stored_config.get(key, default)
entry = ctk.CTkEntry(config_tab, textvariable=ctk.StringVar(value=str(value)))
entry.grid(row=i, column=1, padx=10, pady=5)
entries[key] = entry
apply_button = ctk.CTkButton(config_tab, text="Apply changes", command=lambda: apply_changes(entries))
apply_button.grid(row=len(default_config), column=0, columnspan=2, pady=(7, 5))
settings_window_instance.update_idletasks()
window_width = settings_window_instance.winfo_reqwidth()
window_height = settings_window_instance.winfo_reqheight()
screen_width = settings_window_instance.winfo_screenwidth()
screen_height = settings_window_instance.winfo_screenheight()
center_x = int(screen_width / 2 - window_width / 2)
center_y = int(screen_height / 2 - window_height / 2) - 40
settings_window_instance.geometry(f'+{center_x}+{center_y}')
def on_close():
# Instead of destroying the window, just hide it
settings_window_instance.withdraw()
settings_window_instance.protocol("WM_DELETE_WINDOW", on_close)
settings_window_instance.resizable(False, False)
settings_window_instance.mainloop()
# Change system tray icon
def icon_change(name):
script_folder = getcwd()
icon_path = path.join(script_folder, "Icons", name)
image = open_image(icon_path)
tray_icon.icon = image
def icon_change_safe(name):
if 'tray_icon' in globals() and tray_icon is not None:
icon_path = get_icon_path(name)
image = open_image(icon_path)
tray_icon.icon = image
def get_icon_path(icon_name):
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
# Si estamos ejecutando el archivo .exe empaquetado
base_path = sys._MEIPASS
else:
# Si estamos ejecutando el script .py o de otra forma que no defina __file__
if '__file__' in globals():
base_path = os.path.dirname(os.path.abspath(__file__))
else:
base_path = os.path.dirname(os.path.abspath(sys.argv[0]))
icon_path = os.path.join(base_path, 'Icons', icon_name)
return icon_path
# Stop ping test and icon main thread thread when click on exit
def on_right_click_exit(icon, item):
global stop_ping_test
stop_ping_test = True
icon.stop()
def on_right_click_settings(icon, item):
Thread(target=open_settings_window).start()
#Updates tooltip statstics
def update_statistics(successful_pings, packet_loss, response_times,server_to_ping):
if successful_pings > 0:
average_response_time = sum(response_times) / successful_pings
min_response_time = min(response_times)
max_response_time = max(response_times)
else:
average_response_time = 0
min_response_time = 0
max_response_time = 0
packet_loss_percentage = (packet_loss / (successful_pings + packet_loss)) * 100
tooltip = (
f'[Packet statistics]\n'
f'Sent: {successful_pings + packet_loss}\n'
f'Lost: {packet_loss} ({packet_loss_percentage:.2f}%)\n'
f'To: {server_to_ping} \n'
f'Avg. RTT: {average_response_time:.2f}ms\n'
f'Min. RTT: {min_response_time:.2f}ms\n'
f'Max. RTT: {max_response_time:.2f}ms'
)
return tooltip
def update_statistics_safe(successful_pings, packet_loss, response_times,server_to_ping):
if successful_pings > 0:
average_response_time = sum(response_times) / successful_pings
min_response_time = min(response_times)
max_response_time = max(response_times)
else:
average_response_time = 0
min_response_time = 0
max_response_time = 0
packet_loss_percentage = (packet_loss / (successful_pings + packet_loss)) * 100
tooltip = (
f'[Packet statistics]\n'
f'Sent: {successful_pings + packet_loss}\n'
f'Lost: {packet_loss} ({packet_loss_percentage:.2f}%)\n'
f'To: {server_to_ping} \n'
f'Avg. RTT: {average_response_time:.2f}ms\n'
f'Min. RTT: {min_response_time:.2f}ms\n'
f'Max. RTT: {max_response_time:.2f}ms'
)
update_tray_tooltip_safe(tooltip)
#Define ping test
def ping_test(icon):
global settings_changed, stop_ping_test, tray_icon
global ping_count, seconds_between_pings, time_until_lost
global server_to_ping, perfect_threshold, good_threshold, medium_threshold
response_time_pattern = re.compile(r'(\d+(?:\.\d+)?)ms')
while not stop_ping_test:
if settings_changed:
ping_count, seconds_between_pings, time_until_lost, server_to_ping, perfect_threshold, good_threshold, medium_threshold = read_settings()
settings_changed = False
successful_pings = 0
packet_loss = 0
response_times = []
i = 0
while i < ping_count and not stop_ping_test:
result = run(['ping', '-n', '1', '-w', str(time_until_lost), server_to_ping], stdout=PIPE, creationflags=CREATE_NO_WINDOW)
encoding = chardet.detect(result.stdout)['encoding']
decoded_output = result.stdout.decode(encoding)
match = response_time_pattern.search(decoded_output)
sleep(seconds_between_pings)
#print(decoded_output + "Pinging to: " +server_to_ping)
print("Pinging to: " + server_to_ping)
if match:
successful_pings += 1
response_time = float(match.group(1))
response_times.append(response_time)
else:
packet_loss += 1
i += 1
packet_loss_percentage = (packet_loss / ping_count) * 100
if packet_loss_percentage < perfect_threshold:
icon_change_safe('Perfect.png')
elif packet_loss_percentage < good_threshold:
icon_change_safe('Good.png')
elif packet_loss_percentage < medium_threshold:
icon_change_safe('Medium.png')
else:
icon_change_safe('Bad.png')
update_statistics_safe(successful_pings, packet_loss, response_times, server_to_ping)
#Reads the current settings from the Settings.txt file.
def read_settings():
registry_path = r'Software\PingTester'
default_settings = {
"ping_count": "10",
"seconds_between_pings": "0.33",
"max_response_time": "150",
"server_to_ping": "google.com",
"perfect_threshold": "9",
"good_threshold": "18",
"medium_threshold": "28"
}
try:
with reg.OpenKey(reg.HKEY_CURRENT_USER, registry_path, 0, reg.KEY_READ) as registry_key:
settings_str, _ = reg.QueryValueEx(registry_key, 'Settings')
settings = json.loads(settings_str) # Deserialize the JSON string to a dictionary
except (OSError, json.JSONDecodeError):
print("Registry entry not found or invalid. Using default settings.")
settings = default_settings # Use default settings without saving them to the registry
# Extract individual settings, converting to the appropriate type
ping_count = int(settings["ping_count"])
seconds_between_pings = float(settings["seconds_between_pings"])
max_response_time = int(settings["max_response_time"])
server_to_ping = settings["server_to_ping"]
perfect_threshold = int(settings["perfect_threshold"])
good_threshold = int(settings["good_threshold"])
medium_threshold = int(settings["medium_threshold"])
return ping_count, seconds_between_pings, max_response_time, server_to_ping, perfect_threshold, good_threshold, medium_threshold
def on_right_click_coffe(_=None):
ko_fi_url = 'https://ko-fi.com/gdsdev'
webbrowser.open(ko_fi_url)
# Set up system tray icon and context menu
# Reading settings and on_right_click_coffee function unchanged
def setup_tray_icon():
global tray_icon
menu = Menu(
MenuItem('Like the app? Buy me a coffee!', on_right_click_coffe),
MenuItem('Settings', on_right_click_settings),
MenuItem('Exit', on_right_click_exit)
)
ping_count, seconds_between_pings, max_response_time, server_to_ping, perfect_threshold, good_threshold, medium_threshold = read_settings()
estimated_test_time = ping_count * seconds_between_pings
ftooltip = (
f'[Performing test]\n'
f'To: {server_to_ping}\n'
f'Pings: {ping_count}\n'
f'Interval: {seconds_between_pings}s\n'
f'Timeout: {max_response_time}ms\n'
f'Est. test time: {estimated_test_time:.1f}'
)
icon_path = path.join(path.dirname(path.abspath(__file__)), 'Icons', 'Waiting.png')
icon = open_image(icon_path)
tray_icon = Icon('Testing', icon, ftooltip, menu=menu)
tray_icon.max_tooltip_width = 350
return tray_icon
settings_changed = True
stop_ping_test = False
settings_window_instance = None
if __name__ == "__main__":
tray_icon = setup_tray_icon()
Thread(target=lambda: ping_test(tray_icon)).start()
try:
print("Sale por 0")
tray_icon.run()
except Exception as e:
print(f"Unexpected error occurred: {e}")
finally:
tray_icon.stop()
os._exit(1)