Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  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. matchList = ['', '', '']
  10. ping = ""
  11.  
  12.  
  13. def main_loop(gui_queue):
  14. global matchList, ping
  15. sg.change_look_and_feel('Dark Blue 3')
  16. layout = [
  17. [sg.Text('Enter domain name of what you want to ping')],
  18. [sg.Input(size=(37, 1), key='dn')],
  19. [sg.Ok(), sg.Cancel()]
  20. ]
  21.  
  22. window = sg.Window('Domain name entry', layout)
  23. event, value = window.read()
  24. if event is None or event == 'Cancel':
  25. return
  26.  
  27. matchList[0] = value['dn']
  28. if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  29. matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True).stdout.decode()))
  30. elif platform == 'win32':
  31. matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True).stdout.decode()))
  32.  
  33. window.close()
  34. del window
  35.  
  36. sg.set_options(element_padding=(0,0))
  37.  
  38. sg.change_look_and_feel('Dark Blue 3')
  39. layout = [
  40. [sg.Text("The IP address of hostname '" + matchList[0] + "' is: " + matchList[1], size=(100, 1), )],
  41. [sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING),
  42. background_color='white', key='graph')],
  43. [sg.Button('Exit', button_color=('black', 'white'))]
  44. ]
  45.  
  46. window = sg.Window(matchList[0], layout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
  47. graph = window['graph']
  48.  
  49. threading.Thread(target=ping, args=(10, gui_queue), daemon=True).start()
  50.  
  51. i, j = 0, 1
  52. prev_x, prev_y = 0, 0
  53. graph_value = 0
  54.  
  55. while True:
  56. graph_value = 0
  57. event, values = window.read(timeout=25)
  58. if event == 'Exit' or event is None:
  59. break
  60.  
  61. while True:
  62. try:
  63. graph_value = gui_queue.get_nowait()
  64. except queue.Empty:
  65. break
  66.  
  67. if graph_value > 0:
  68. print("Iteration #" + str(j))
  69.  
  70. if graph_value > MAX_PING:
  71. graph_value = MAX_PING
  72. if graph_value < 0:
  73. graph_value = 0
  74.  
  75. new_x, new_y = i, graph_value
  76.  
  77. if i >= NO_OF_PINGS:
  78. graph.move(-STEP_SIZE, 0)
  79. prev_x = prev_x - STEP_SIZE
  80.  
  81. graph.draw_line((prev_x, prev_y), (new_x, new_y), color='red')
  82. prev_x, prev_y = new_x, new_y
  83. i += STEP_SIZE if i < NO_OF_PINGS else 0
  84. j += 1
  85.  
  86. window.close()
  87.  
  88. def ping(run_freq, gui_queue):
  89. while True:
  90. try:
  91. time.sleep(run_freq/1000)
  92. ping = ""
  93. if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  94. ping = subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True, text=True)
  95. elif platform == 'win32':
  96. ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True, text=True)
  97. cmdOutput = ''.join(re.findall('time=(\d+)', ping.stdout))
  98. if cmdOutput.isdigit() == True:
  99. gui_queue.put(int(cmdOutput))
  100. except ValueError:
  101. continue
  102.  
  103.  
  104. if __name__ == '__main__':
  105. gui_queue = queue.Queue()
  106.  
  107. main_loop(gui_queue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement