GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update network_check_app · theFest/python-code@999aa58

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • graphical-user-interface/network_check_app
    • network_check_app.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎graphical-user-interface/network_check_app/network_check_app.py‎

Copy file name to clipboardExpand all lines: graphical-user-interface/network_check_app/network_check_app.py
+105-61Lines changed: 105 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@
33
import subprocess
44
import ctypes
55
import json
6-
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QSystemTrayIcon, QMenu, QAction, QStyleFactory, QMessageBox, QTextEdit, QVBoxLayout, QHBoxLayout, QFileDialog, QSlider, QFormLayout, QGroupBox, QCheckBox
6+
from PyQt5.QtWidgets import (
7+
QApplication,
8+
QMainWindow,
9+
QWidget,
10+
QLabel,
11+
QLineEdit,
12+
QPushButton,
13+
QSystemTrayIcon,
14+
QMenu,
15+
QAction,
16+
QStyleFactory,
17+
QMessageBox,
18+
QTextEdit,
19+
QVBoxLayout,
20+
QHBoxLayout,
21+
QFileDialog,
22+
QSlider,
23+
QFormLayout,
24+
QGroupBox,
25+
QCheckBox,
26+
)
727
from PyQt5.QtCore import QTimer, Qt
828
from PyQt5.QtGui import QIcon, QColor, QPalette, QPixmap
929
import ping3
@@ -12,116 +32,126 @@
1232
import requests
1333
from fontawesome import icons
1434

35+
if sys.platform == "win32":
36+
import ctypes
37+
38+
1539
class NetworkCheckApp(QMainWindow):
1640
def __init__(self):
1741
super().__init__()
1842

1943
self.setWindowTitle("Network Check Application")
2044
self.setGeometry(100, 100, 600, 300)
21-
45+
2246
self.init_ui()
23-
47+
2448
self.tray_icon = QSystemTrayIcon(self)
2549
self.tray_icon.setIcon(QIcon("icon.png"))
2650
self.tray_icon.setVisible(True)
27-
51+
2852
self.tray_menu = QMenu()
2953
self.show_action = QAction("Show", self)
3054
self.exit_action = QAction("Exit", self)
3155
self.tray_menu.addAction(self.show_action)
3256
self.tray_menu.addAction(self.exit_action)
33-
57+
3458
self.show_action.triggered.connect(self.show_window)
3559
self.exit_action.triggered.connect(self.exit_app)
36-
60+
3761
self.tray_icon.setContextMenu(self.tray_menu)
38-
62+
3963
self.output_text = ""
40-
64+
4165
def init_ui(self):
4266
self.central_widget = QWidget(self)
4367
self.setCentralWidget(self.central_widget)
44-
68+
4569
self.create_layout()
46-
70+
4771
def create_layout(self):
4872
layout = QVBoxLayout()
49-
73+
5074
self.output_window = QTextEdit(self.central_widget)
5175
self.output_window.setReadOnly(True)
5276
layout.addWidget(self.output_window)
53-
77+
5478
settings_group = QGroupBox("Settings", self.central_widget)
5579
settings_layout = QFormLayout(settings_group)
56-
80+
5781
self.host_input = QLineEdit(self.central_widget)
5882
self.host_input.setPlaceholderText("Host (IP or Domain)")
59-
83+
6084
self.interval_input = QLineEdit(self.central_widget)
6185
self.interval_input.setPlaceholderText("Ping Interval (seconds)")
62-
86+
6387
self.timeout_slider = QSlider(Qt.Horizontal, self.central_widget)
6488
self.timeout_slider.setRange(1, 5000)
6589
self.timeout_slider.setValue(1000)
6690
self.timeout_slider.valueChanged.connect(self.update_timeout_label)
6791
self.timeout_value_label = QLabel("1000", self.central_widget)
68-
69-
self.continuous_ping_checkbox = QCheckBox("Continuous Ping", self.central_widget)
70-
92+
93+
self.continuous_ping_checkbox = QCheckBox(
94+
"Continuous Ping", self.central_widget
95+
)
96+
7197
self.traceroute_button = QPushButton("Traceroute", self.central_widget)
7298
self.traceroute_button.clicked.connect(self.start_traceroute)
73-
99+
74100
self.geolocation_button = QPushButton("Geolocation Lookup", self.central_widget)
75101
self.geolocation_button.clicked.connect(self.start_geolocation_lookup)
76-
102+
77103
settings_layout.addRow("Host:", self.host_input)
78104
settings_layout.addRow("Interval:", self.interval_input)
79105
settings_layout.addRow("Timeout (ms):", self.timeout_slider)
80106
settings_layout.addRow("", self.timeout_value_label)
81107
settings_layout.addRow("", self.continuous_ping_checkbox)
82108
settings_layout.addRow("", self.traceroute_button)
83109
settings_layout.addRow("", self.geolocation_button)
84-
110+
85111
settings_group.setLayout(settings_layout)
86-
112+
87113
control_layout = QHBoxLayout()
88-
114+
89115
self.ping_button = QPushButton("Ping Test", self.central_widget)
90-
ping_icon = QPixmap(icons['network-wired']).scaledToHeight(20)
116+
ping_icon = QPixmap(icons["network-wired"]).scaledToHeight(20)
91117
self.ping_button.setIcon(QIcon(ping_icon))
92118
self.ping_button.clicked.connect(self.start_ping)
93119

94120
self.clear_button = QPushButton("Clear Output", self.central_widget)
95121
self.clear_button.clicked.connect(self.clear_output)
96-
122+
97123
self.save_button = QPushButton("Save Log", self.central_widget)
98124
self.save_button.clicked.connect(self.save_log)
99-
125+
100126
self.minimize_button = QPushButton("Minimize to Tray", self.central_widget)
101127
self.minimize_button.clicked.connect(self.minimize_to_tray)
102-
128+
103129
control_layout.addWidget(self.ping_button)
104130
control_layout.addWidget(self.clear_button)
105131
control_layout.addWidget(self.save_button)
106132
control_layout.addWidget(self.minimize_button)
107-
133+
108134
layout.addWidget(settings_group)
109135
layout.addLayout(control_layout)
110136
self.central_widget.setLayout(layout)
111-
137+
112138
def update_timeout_label(self):
113139
self.timeout_value_label.setText(str(self.timeout_slider.value()))
114-
140+
115141
def start_ping(self):
116-
if hasattr(self, 'timer') and self.timer.isActive():
142+
if hasattr(self, "timer") and self.timer.isActive():
117143
self.stop_ping()
118144
else:
119145
host = self.host_input.text()
120146
interval = self.interval_input.text()
121147
timeout = self.timeout_slider.value() / 1000.0
122148

123149
if not host or not interval.isdigit():
124-
QMessageBox.critical(self, "Error", "Invalid input. Please enter a valid host and interval.")
150+
QMessageBox.critical(
151+
self,
152+
"Error",
153+
"Invalid input. Please enter a valid host and interval.",
154+
)
125155
return
126156

127157
self.timer = QTimer(self)
@@ -130,86 +160,99 @@ def start_ping(self):
130160
self.ping_button.setText("Stop Ping")
131161
self.continuous_ping_checkbox.setEnabled(False)
132162
self.timeout_slider.setEnabled(False)
133-
163+
134164
def stop_ping(self):
135-
if hasattr(self, 'timer') and self.timer.isActive():
165+
if hasattr(self, "timer") and self.timer.isActive():
136166
self.timer.stop()
137167
self.ping_button.setText("Ping Test")
138168
self.continuous_ping_checkbox.setEnabled(True)
139169
self.timeout_slider.setEnabled(True)
140-
170+
141171
def ping(self):
142172
host = self.host_input.text()
143173
timeout = self.timeout_slider.value() / 1000.0
144-
174+
145175
response_time = ping3.ping(host, timeout=timeout, unit="ms")
146-
176+
147177
if response_time is not None:
148-
self.append_to_output(f"Ping response from {host}: {response_time:.2f} ms", "green")
178+
self.append_to_output(
179+
f"Ping response from {host}: {response_time:.2f} ms", "green"
180+
)
149181
else:
150182
self.append_to_output(f"Request timed out for {host}", "red")
151-
183+
152184
def start_traceroute(self):
153185
host = self.host_input.text()
154-
186+
155187
if not host:
156-
QMessageBox.critical(self, "Error", "Invalid input. Please enter a valid host.")
188+
QMessageBox.critical(
189+
self, "Error", "Invalid input. Please enter a valid host."
190+
)
157191
return
158-
192+
159193
try:
160194
command = ["tracert", host]
161195
result = subprocess.run(command, stdout=subprocess.PIPE, text=True)
162196
self.append_to_output(result.stdout, "white")
163197
except subprocess.CalledProcessError:
164198
self.append_to_output("Traceroute failed.", "red")
165-
199+
166200
def start_geolocation_lookup(self):
167201
host = self.host_input.text()
168-
202+
169203
if not host:
170-
QMessageBox.critical(self, "Error", "Invalid input. Please enter a valid host.")
204+
QMessageBox.critical(
205+
self, "Error", "Invalid input. Please enter a valid host."
206+
)
171207
return
172-
208+
173209
try:
174210
ip = socket.gethostbyname(host)
175211
ipwhois = IPWhois(ip)
176212
results = ipwhois.lookup_rdap()
177-
country = results.get('asn_country_code', 'Unknown')
178-
self.append_to_output(f"Geolocation for {host}: Country - {country}", "green")
213+
country = results.get("asn_country_code", "Unknown")
214+
self.append_to_output(
215+
f"Geolocation for {host}: Country - {country}", "green"
216+
)
179217
except:
180218
self.append_to_output("Geolocation lookup failed.", "red")
181-
219+
182220
def append_to_output(self, text, color):
183221
self.output_text += f'<span style="color: {color}">{text}</span><br>'
184222
self.output_window.setHtml(self.output_text)
185-
self.output_window.verticalScrollBar().setValue(self.output_window.verticalScrollBar().maximum())
186-
223+
self.output_window.verticalScrollBar().setValue(
224+
self.output_window.verticalScrollBar().maximum()
225+
)
226+
187227
def show_window(self):
188228
self.show()
189-
229+
190230
def exit_app(self):
191231
self.tray_icon.hide()
192232
self.close()
193-
233+
194234
def clear_output(self):
195235
self.output_text = ""
196236
self.output_window.clear()
197-
237+
198238
def save_log(self):
199239
options = QFileDialog.Options()
200240
options |= QFileDialog.ReadOnly
201-
file_name, _ = QFileDialog.getSaveFileName(self, "Save Log", "", "Text Files (*.txt);;All Files (*)", options=options)
202-
241+
file_name, _ = QFileDialog.getSaveFileName(
242+
self, "Save Log", "", "Text Files (*.txt);;All Files (*)", options=options
243+
)
244+
203245
if file_name:
204246
with open(file_name, "w") as file:
205247
file.write(self.output_text)
206-
248+
207249
def minimize_to_tray(self):
208250
self.hide()
209251

252+
210253
if __name__ == "__main__":
211254
app = QApplication(sys.argv)
212-
255+
213256
app.setStyle(QStyleFactory.create("Fusion"))
214257
palette = QPalette()
215258
palette.setColor(QPalette.Window, QColor(53, 53, 53))
@@ -227,9 +270,10 @@ def minimize_to_tray(self):
227270
palette.setColor(QPalette.HighlightedText, Qt.black)
228271
app.setPalette(palette)
229272

230-
fw_app_id = u'fw.network.checkapp.1'
231-
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(fw_app_id)
232-
273+
if sys.platform == "win32":
274+
fw_app_id = "fw.network.checkapp.1"
275+
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(fw_app_id)
276+
233277
window = NetworkCheckApp()
234278
window.show()
235279
sys.exit(app.exec_())

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.