@AndroidNoobie's answer works fine. I am adding this just in case you want the execution to be async, you could use subprocess.Popen
instead of subprocess.run
.
The UI freezes until the run
execution is complete. If you don't want that to happen, I would recommend using subprocess.Popen
def PING_CLIENT():
HOST = PING_ENTRY.get()
command = "ping {} -n 30 -t".format(HOST)
#subprocess.run(command, shell=True)
subprocess.Popen(command, shell=True)
From another SO answer:
The main difference is that subprocess.run
executes a command and waits for it to finish, while with subprocess.Popen
you can continue doing your stuff while the process finishes and then just repeatedly call subprocess.communicate yourself to pass and receive data to your process.
EDIT: Added code to make the ping stop after 30 trials.
To make your code stop after a specific number of packets use the below code.
Windows:
command = "ping -n 30 {}".format(HOST)
pro = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
print(pro.communicate()[0]) # prints the stdout
Ubuntu:
command = "ping -c 30 {}".format(HOST)
pro = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
print(pro.communicate()[0]) # prints the stdout
-t basically pings indefinitely in windows.That's why you weren't able to stop it.