首页 > 解决方案 > Ping 操作在 pycharm 中运行良好,但在 jupyter notebook 中无法运行

问题描述

我有一段代码将使用代码中提到的特定 IP 地址 ping 2 台服务器,它会以漂亮表的形式列出 ping 所花费的时间。当我在 pycharm 中执行相同的代码时,它工作正常。但是,如果我在 jupyter notebook 中执行它,代码不会被执行,因此 ping 的时间不会在漂亮的表中更新。任何人都可以告诉我问题是什么?

这是我在控制台中得到的输出: Table For Ping Timings: +-----------------+---------------- -----+ | 8.8.8.8 的时间 | 时间为 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 | +------------------+----------+ | | 20 | 229 | | 27 | 235 | | 32 | 230 | +------------------+----------+ | | 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()

这是我在 pycharm 中得到的输出

这是输出: Table For Ping Timings: +-----------------+-------------------- -+ | 8.8.8.8 的时间 | 时间为 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 | +------------------+----------+ | | 20 | 229 | | 27 | 235 | | 32 | 230 | +------------------+----------+ | | 20 | 229 | | 27 | 235 | | 32 | 230 | +------------------+----------+

标签: python-3.xpycharmjupyter-notebookpingprettytable

解决方案


推荐阅读