Last active
August 29, 2015 14:13
-
-
Save lancepioch/614a35cf9d1ba050d994 to your computer and use it in GitHub Desktop.
[Python 3] Pinger is a tiny window that tells you your current average ping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, subprocess, tkinter, threading | |
fontsize = 20 | |
timeregex = r'(?<=time=)\d+(?=ms )' # regex to retrieve the # ms | |
website = 'google.com' # url to ping | |
pinglimit = 10 # number of pings to average | |
pinginterval = 500 # ms delay between pings | |
pings = [] # array to hold pings | |
def avg(l): | |
length = len(l) | |
if length == 0: | |
return length | |
return int(sum(l) / float(len(l))) | |
def getPing(): | |
cmd = ['ping', website, '-n', '1'] | |
startupinfo = subprocess.STARTUPINFO() | |
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW | |
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, startupinfo=startupinfo) | |
for line in proc.stdout: | |
ping = re.findall(timeregex, str(line)) | |
if len(ping) > 0: | |
if len(pings) > pinglimit: | |
del pings[0] | |
pings.append(int(ping[0])) | |
proc.wait() | |
return "-" | |
class mainForm(): | |
def __init__(self): | |
self.root = tkinter.Tk() | |
self.label = tkinter.Label(text='', font=('Helvetica', fontsize)) | |
self.label.pack() | |
self.update_clock() | |
self.root.mainloop() | |
def update_clock(self): | |
threading.Thread(target=getPing).start() | |
self.label.configure(text=str(avg(pings))) | |
self.root.after(pinginterval, self.update_clock) | |
if __name__ == '__main__': | |
app = mainForm() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment