SHARE
TWEET

Current, working file

a guest Dec 15th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import subprocess, time, re, math, threading, queue
  2. import PySimpleGUI as sg
  3. from sys import platform
  4.  
  5. STEP_SIZE = 1
  6. NO_OF_PINGS = 500
  7. MAX_PING = 250
  8. CANVAS_SIZE = (800, 600)
  9.  
  10. ping = ""
  11.  
  12. class justAList():
  13.     def __init__(self):
  14.         self.matchList = ['', '', '']
  15.  
  16.  
  17. def main_loop(matchClass, gui_queue):
  18.     lowestPing = averagePing = highestPing = totalPing = 0
  19.     sg.change_look_and_feel('Dark Blue 3')
  20.     inputLayout = [
  21.         [sg.Text('Enter domain name of what you want to ping')],
  22.         [sg.InputText(size=(37, 1), key='dn')],
  23.         [sg.Submit(), sg.Cancel()]
  24.     ]
  25.  
  26.     inputWindow = sg.Window('Domain name entry', inputLayout)
  27.     event, value = inputWindow.read()
  28.     if event is None or event == 'Cancel':
  29.         return
  30.  
  31.     matchClass.matchList[0] = value['dn']
  32.     if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  33.         matchClass.matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-c', '1', matchClass.matchList[0]], capture_output=True).stdout.decode()))
  34.     elif platform == 'win32':
  35.         matchClass.matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-n', '1', matchClass.matchList[0]], capture_output=True).stdout.decode()))
  36.  
  37.     inputWindow.close()
  38.     del inputWindow
  39.  
  40.     sg.set_options(element_padding=(0,0))
  41.  
  42.     sg.change_look_and_feel('Dark Blue 3')
  43.     graphLayout = [
  44.         [sg.Text("          The IP address of hostname '" + matchClass.matchList[0] + "' is: " + matchClass.matchList[1], size=(100, 1), )],
  45.         [sg.Text("")],
  46.         [sg.Text("Ping\n(ms)", size=(5, 2)), sg.Graph((20, 600), (0, 0), (20, MAX_PING), background_color='white', key='yAxis'),
  47.         sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING), background_color='white', key='mainGraph')],
  48.         [sg.Text(size=(5, 1)), sg.Graph((822, 20), (0, 0), (NO_OF_PINGS, 20), background_color='white', key='xAxis')],
  49.         [sg.Text("          Number of pings: 0\t\t", key='nP'), sg.Text("Lowest ping: " + str(lowestPing) + '\t\t', key='lP'),
  50.         sg.Text("Average ping: " + str(averagePing) + '\t\t', key='aP'), sg.Text("Highest ping: " + str(highestPing), key='hP')],
  51.         [sg.Button('Exit', button_color=('black', 'white'))]
  52.     ]
  53.  
  54.     graphWindow = sg.Window(matchClass.matchList[0], graphLayout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
  55.     mainGraph = graphWindow['mainGraph']
  56.     xGraph = graphWindow['xAxis']
  57.     yGraph = graphWindow['yAxis']
  58.  
  59.  
  60.     yGraph.DrawLine((20, 0), (20, MAX_PING))
  61.     xGraph.DrawLine((13, 19), (NO_OF_PINGS, 19))
  62.  
  63.     for x in range(20, NO_OF_PINGS - 1, 20):
  64.         xGraph.DrawLine((x, 10), (x, 20))
  65.         if x != 0:
  66.             xGraph.DrawText(x, (x, 5), color='green')
  67.  
  68.     for y in range(20, MAX_PING - 1, 20):
  69.         yGraph.DrawLine((10, y), (20, y))
  70.         if y != 0:
  71.             yGraph.DrawText(y, (10, y-3), color='blue')
  72.  
  73.     threading.Thread(target=ping, args=(10, gui_queue, matchClass), daemon=True).start()
  74.  
  75.     i, j = 0, 1
  76.     prev_x, prev_y = 0, 0
  77.     graph_value = 0
  78.  
  79.     while True:
  80.         graph_value = 0
  81.         event, values = graphWindow.read(timeout=25)
  82.         if event == 'Exit' or event is None:
  83.             break
  84.  
  85.         while True:
  86.             try:
  87.                 graph_value = gui_queue.get_nowait()
  88.             except queue.Empty:
  89.                 break
  90.  
  91.         if graph_value > 0:
  92.  
  93.             if j == 1:
  94.                 lowestPing = averagePing = highestPing = graph_value
  95.             else:
  96.                 if lowestPing > graph_value:
  97.                     lowestPing = graph_value
  98.                 if highestPing < graph_value:
  99.                     highestPing = graph_value
  100.             totalPing += graph_value
  101.             averagePing = '{:.2f}'.format(totalPing / j, 2)
  102.  
  103.             graphWindow['nP'].Update("          Number of pings: " + str(j) + '\t\t')
  104.             graphWindow['lP'].Update("Lowest ping: " + str(lowestPing) + '\t\t')
  105.             graphWindow['aP'].Update("Average ping: " + averagePing + '\t')
  106.             graphWindow['hP'].Update("Highest ping: " + str(highestPing))
  107.  
  108.             #print("Iteration #" + str(j) + " - highest ping is " + str(highestPing))
  109.  
  110.             if graph_value > MAX_PING:
  111.                 graph_value = MAX_PING
  112.             if graph_value < 0:
  113.                 graph_value = 0
  114.  
  115.             new_x, new_y = i, graph_value
  116.  
  117.             if i >= NO_OF_PINGS:
  118.                 mainGraph.move(-STEP_SIZE, 0)
  119.                 prev_x = prev_x - STEP_SIZE
  120.  
  121.             mainGraph.draw_line((prev_x, prev_y), (new_x, new_y), color='red')
  122.             prev_x, prev_y = new_x, new_y
  123.             i += STEP_SIZE if i < NO_OF_PINGS else 0
  124.             j += 1
  125.  
  126.     graphWindow.close()
  127.     del graphWindow
  128.  
  129. def ping(run_freq, gui_queue, matchClass):
  130.     while True:
  131.         try:
  132.             time.sleep(run_freq/1000)
  133.             ping = ""
  134.             if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  135.                 ping = subprocess.run(['ping', '-c', '1', matchClass.matchList[0]], capture_output=True, text=True)
  136.             elif platform == 'win32':
  137.                 ping = subprocess.run(['ping', '-n', '1', matchClass.matchList[0]], capture_output=True, text=True)
  138.             cmdOutput = ''.join(re.findall('time=(\d+)', ping.stdout))
  139.             if cmdOutput.isdigit() == True:
  140.                 gui_queue.put(int(cmdOutput))
  141.         except ValueError:
  142.             continue
  143.  
  144.  
  145. if __name__ == '__main__':
  146.     matchClass = justAList()
  147.  
  148.     gui_queue = queue.Queue()
  149.  
  150.     main_loop(matchClass, gui_queue)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top