I have a piece of code that will ping 2 servers with a specific IP addresses mentioned in the code and it will list the time taken for the pings in the form of a prettytable. When i execute the same code in pycharm it is working fine .However if i execute it in jupyter notebook, the code is not getting executed and as a result the time of the pings is not updated in the pretty table.Can any body tell me what the issue is?
This is the output i got in console: Table For Ping Timings: +------------------+---------------------+ | TIME FOR 8.8.8.8 | TIME FOR 13.32.32.0 | +------------------+---------------------+ | 32 | 236 | | 19 | 234 | | 46 | 227 | | 45 | 241 | | 29 | 239 | | 33 | 234 | | 21 | 230 | | 22 | 226 | | 26 | 235 | | 26 | 235 | | 24 | 238 | | 20 | 243 | | 26 | 234 | | 26 | 233 | | 26 | 238 | | 27 | 232 | | 123 | 240 | | 29 | 233 | | 39 | 241 | | 29 | 453 | | 26 | 234 | | 43 | 236 | | 30 | 255 | | 20 | 233 | | 35 | 238 | | 21 | 245 | | 36 | 232 | | 47 | 262 | | 23 | 242 | | 28 | 243 | | 25 | 246 | | 24 | 232 | | 34 | 235 | | 55 | 237 | | 37 | 227 | | 36 | 240 | | 21 | 255 | | 27 | 227 | | 33 | 235 | | 38 | 235 | | 25 | 251 | | 28 | 249 | | 19 | 234 | | 27 | 233 | | 27 | 237 | | 40 | 244 | | 24 | 232 | | 20 | 229 | | 27 | 235 | | 32 | 230 | +------------------+---------------------+
import subprocess
import re
import matplotlib.pyplot as plt
import sys
from prettytable import PrettyTable
def req():
print("pinging 8.8.8.X....")
print()
h = 0
try:
Q = [line.rpartition('_')[-1] for line in subprocess.check_output("ping -n 50 8.8.8.8").splitlines()[2:-4]]
except:
Q = "Request timed out."
i = 0
j = 0
g = 0
k = 0
w = 0
k = 0
x = []
while i < 50:
if Q != "Request timed out.":
while w < 50:
comp = re.search(r'time=\d{1,4}', Q[i])
if hasattr(comp, 'group'):
T = comp.group(0)[5:9]
x.append(T)
w = w + 1
i = i + 1
else:
T = 0
x.append(T)
w = w + 1
i = i + 1
k = k + 1
if k == 5:
plt.plot(x, marker='o')
break
plt.plot(x, marker='o')
else:
T = 0
x.append(T)
i = i + 1
g = g + 1
if g == 5:
plt.plot(x, marker='o')
s = 1
while s < 46:
x.append('-')
s = s + 1
i = i + 1
g = g + 1
print("pings for 8.8.8.X are done")
print()
print("pinging 13.32.32.0")
try:
A = [line.rpartition('_')[-1] for line in subprocess.check_output("ping -n 50 13.32.32.0").splitlines()[2:-4]]
except:
A = "Request timed out."
n = 0
d = 0
e = 0
l = 0
y = []
while n < 50:
if A != "Request timed out.":
while d < 50:
comp = re.search(r'time=\d{1,4}', A[n])
if hasattr(comp, 'group'):
B = comp.group(0)[5:9]
y.append(B)
n = n + 1
d = d + 1
else:
B = 0
y.append(B)
n = n + 1
d = d + 1
l = l + 1
if l == 5:
plt.plot(y, marker='o')
break
plt.plot(y, marker='o')
else:
B = 0
y.append(B)
n = n + 1
e = e + 1
if e == 5:
plt.plot(y, marker='o')
t = 1
while t < 46:
y.append('-')
t = t + 1
n = n + 1
e = e + 1
print()
print("pings for 13.32.32.0 are done")
print()
print("Table For Ping Timings:")
u = 0
table = PrettyTable(["TIME FOR 8.8.8.8", "TIME FOR 13.32.32.0"])
while u < 50:
table.add_row([x[u], y[u]])
u = u + 1
print(table)
plt.ylabel("TIME IN ms")
plt.xlabel("NUMBER OF PINGS")
plt.title("GRAPH to Display Latency")
plt.margins(0.025)
plt.legend(['ping for 8.8.8.8', 'ping for 13.32.32.0'], loc='upper left')
if len(sys.argv) == 2:
try:
j = sys.argv[1].split('.')
if j[1] == ".gif":
print()
print("FILE NOT SAVED: INCORRECT FILE FORMAT:FORMAT SHOULD BE (name.png/name.jpg)")
plt.show()
else:
plt.savefig(sys.argv[1])
plt.show()
except:
print()
print("ERROR:FILE IS NOT SAVED:EXTENSION IS REQUIRED(.png/.jpg)")
plt.show()
else:
plt.show()
if __name__ == '__main__':
req()
This is the output i am getting in pycharm
This is the output: Table For Ping Timings: +------------------+---------------------+ | TIME FOR 8.8.8.8 | TIME FOR 13.32.32.0 | +------------------+---------------------+ | 32 | 236 | | 19 | 234 | | 46 | 227 | | 45 | 241 | | 29 | 239 | | 33 | 234 | | 21 | 230 | | 22 | 226 | | 26 | 235 | | 26 | 235 | | 24 | 238 | | 20 | 243 | | 26 | 234 | | 26 | 233 | | 26 | 238 | | 27 | 232 | | 123 | 240 | | 29 | 233 | | 39 | 241 | | 29 | 453 | | 26 | 234 | | 43 | 236 | | 30 | 255 | | 20 | 233 | | 35 | 238 | | 21 | 245 | | 36 | 232 | | 47 | 262 | | 23 | 242 | | 28 | 243 | | 25 | 246 | | 24 | 232 | | 34 | 235 | | 55 | 237 | | 37 | 227 | | 36 | 240 | | 21 | 255 | | 27 | 227 | | 33 | 235 | | 38 | 235 | | 25 | 251 | | 28 | 249 | | 19 | 234 | | 27 | 233 | | 27 | 237 | | 40 | 244 | | 24 | 232 | | 20 | 229 | | 27 | 235 | | 32 | 230 | +------------------+---------------------+