Added network_test()

This commit is contained in:
2Shirt 2019-11-10 14:47:56 -07:00
parent ce3a98028a
commit 100757ba69
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
3 changed files with 49 additions and 14 deletions

View file

@ -10,11 +10,6 @@ def main():
state = wk.hw.diags.State()
wk.hw.diags.main_menu()
# Done
print('')
print('Done.')
wk.std.pause('Press Enter to exit...')
if __name__ == '__main__':
try:

View file

@ -10,8 +10,15 @@ from docopt import docopt
from wk.cfg.main import KIT_NAME_FULL
from wk.exe import run_program
from wk.net import (
connected_to_private_network,
ping,
show_valid_addresses,
speedtest,
)
from wk.std import (
Menu,
TryAndPrint,
clear_screen,
color_string,
pause,
@ -132,11 +139,39 @@ def main_menu():
selection = menu.advanced_select()
if 'Audio Test' in selection:
audio_test()
if 'Network Test' in selection:
network_test()
elif 'Quit' in selection:
break
print(f'Sel: {selection}')
print('')
pause()
def network_test():
"""Run network tests."""
clear_screen()
try_and_print = TryAndPrint()
result = try_and_print.run(
'Network connection...', connected_to_private_network, msg_good='OK')
# Bail if not connected
if result['Failed']:
print_warning('Please connect to a network and try again')
pause('Press Enter to return to main menu...')
return
# Show IP address(es)
show_valid_addresses()
# Ping tests
try_and_print.run(
'Internet connection...', ping, msg_good='OK', addr='8.8.8.8')
try_and_print.run(
'DNS resolution...', ping, msg_good='OK', addr='google.com')
# Speedtest
try_and_print.run('Speedtest...', speedtest)
# Done
pause('Press Enter to return to main menu...')
if __name__ == '__main__':

View file

@ -6,7 +6,7 @@ import re
import psutil
from wk.exe import run_program
from wk.std import show_data
from wk.std import GenericError, show_data
# REGEX
@ -18,16 +18,21 @@ REGEX_VALID_IP = re.compile(
# Functions
def is_connected():
"""Check for a valid private IP."""
def connected_to_private_network():
"""Check if connected to a private network.
This checks for a valid private IP assigned to this system.
If one isn't found then an exception is raised.
"""
devs = psutil.net_if_addrs()
for dev in devs.values():
for family in dev:
if REGEX_VALID_IP.search(family.address):
# Valid IP found
return True
# Else
return False
return
# No valid IP found
raise GenericError('Not connected to a network')
def ping(addr='google.com'):