构建一个 GUI 应用程序来使用Python ping 主机
先决条件: Python GUI - Tkinter
在本文中,我们将看到如何使用Python ping 使用 URL 或 IP ping 主机 Python中的模块。该模块提供了一种在Python中 ping 的简单方法。它检查主机是否可用并测量响应需要多长时间。
“在”开始之前,我们需要将此模块安装到您的系统中。
pip install pythonping
GUI 如下所示:
Syntax: ping(‘URL or IP’)
Parameter:
- verbose : enables the verbose mode, printing output to a stream
- timeout : is the number of seconds you wish to wait for a response, before assuming the target is unreachable
- payload : allows you to use a specific payload (bytes)
- size : is an integer that allows you to specify the size of the ICMP payload you desire
代码:
Python3
# import module
from pythonping import ping
# pinging the host
ping('www.google.com', verbose=True)
Python3
# import modules
from tkinter import *
from pythonping import ping
def get_ping():
result = ping(e.get(), verbose=True)
res.set(result)
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
# Variable Classes in tkinter
res = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Enter URL or IP :",
bg="light grey").grid(row=0, sticky=W)
Label(master, text="Result :", bg="light grey").grid(row=1, sticky=W)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="light grey").grid(
row=1, column=1, sticky=W)
e = Entry(master)
e.grid(row=0, column=1)
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Show", command=get_ping)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
mainloop()
输出:
Reply from 142.250.71.4, 9 bytes in 61.09ms
Reply from 142.250.71.4, 9 bytes in 60.24ms
Reply from 142.250.71.4, 9 bytes in 60.22ms
Reply from 142.250.71.4, 9 bytes in 60.04ms
Reply from 142.250.71.4, 9 bytes in 61.09ms
Reply from 142.250.71.4, 9 bytes in 60.24ms
Reply from 142.250.71.4, 9 bytes in 60.22ms
Reply from 142.250.71.4, 9 bytes in 60.04ms
Round Trip Times min/avg/max is 60.04/60.4/61.09 ms
GUI的实现:
使用 Tkinter ping GUI 应用程序
蟒蛇3
# import modules
from tkinter import *
from pythonping import ping
def get_ping():
result = ping(e.get(), verbose=True)
res.set(result)
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
# Variable Classes in tkinter
res = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Enter URL or IP :",
bg="light grey").grid(row=0, sticky=W)
Label(master, text="Result :", bg="light grey").grid(row=1, sticky=W)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="light grey").grid(
row=1, column=1, sticky=W)
e = Entry(master)
e.grid(row=0, column=1)
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Show", command=get_ping)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
mainloop()
输出: