Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import datetime
- import parse
- from timeout_decorator import timeout, TimeoutError
- import pandas
- from bokeh.plotting import figure, ColumnDataSource, curdoc
- MAX_PING = 500
- MAX_TABLE_LENGTH = 20
- @timeout(seconds=MAX_PING/1e3)
- def _ping(host='google.com'):
- r = subprocess.run(['ping', '-c', '1', host], stdout=subprocess.PIPE)
- time_str = r.stdout.decode('utf-8').split('\n')[1].split(' ')[-2]
- return float(parse.parse('time={}', time_str)[0])
- def ping(**kwargs):
- try:
- p = _ping(**kwargs)
- except TimeoutError:
- p = 'NaN'
- return p
- def make_doc(doc):
- source = ColumnDataSource(
- pandas.DataFrame({
- 'time': [],
- 'latency': []}))
- def update():
- new = {'time': [datetime.datetime.now()],
- 'latency': [ping()]}
- source.stream(pandas.DataFrame(new))
- # Setup line figure
- fig_1 = figure(sizing_mode='scale_width',
- x_axis_type='datetime')
- fig_1.line(
- source=source,
- x='time',
- y='latency',
- color='black',
- line_width=2,)
- fig_1.xaxis.axis_label = 'Time'
- fig_1.yaxis.axis_label = 'Round-Trip Time to google.com [ms]'
- fig_1.x_range.follow = "end"
- fig_1.x_range.follow_interval = 6e4
- fig_1.x_range.range_padding = 0
- # Setup Document
- doc.title = "High Street Ping"
- doc.add_periodic_callback(update, 200)
- doc.add_root(fig_1)
- # Create document
- make_doc(curdoc())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement