简体   繁体   English

为什么在运行我的 python ping 脚本时出现找不到主机错误?

[英]Why am i getting a host not found error when running my python ping script?

i made this script a while ago and it was working if i remember correctly but now i get a could not find host error.我不久前制作了这个脚本,如果我没记错的话它可以工作,但现在我得到一个找不到主机错误。 Any help is appreciated.任何帮助表示赞赏。

from tkinter import *
from tkinter import ttk
import socket
import sqlite3
import subprocess




BASE = Tk()
BASE.geometry("400x400")




def PING_CLIENT():
    
    HOST = PING_ENTRY

    command = "ping {} 30 -t".format(HOST)
    subprocess.run(command)
    
    


PING = ttk.Button(BASE, text="Ping IP", command=PING_CLIENT)
                  
PING.place(x=35, y=100, height=30, width=150)

PING_ENTRY = ttk.Entry(BASE)
PING_ENTRY.place(x=200, y=100, height=30, width=150)


BASE.mainloop()

You need to get the value of your Entry widget.您需要获取 Entry 小部件的值。 To do this, call the get() method on the widget.为此,请在小部件上调用get()方法。 You can read more about the Tkinter Entry Widget here .您可以在此处阅读有关 Tkinter 条目小部件的更多信息。

Example:例子:

HOST = PING_ENTRY.get()

Also, I'm not exactly sure what the "30" in your command is supposed to do.另外,我不确定您命令中的“30”应该做什么。 If you intend for it to ping 30 times, you need to add the -n switch beforehand (on Windows) or -c switch (on most Linux distributions).如果您打算 ping 30 次,则需要预先添加-n开关(在 Windows 上)或-c开关(在大多数 Linux 发行版上)。 For example, on Windows:例如,在 Windows 上:

command = "ping {} -n 30 -t".format(HOST)

@AndroidNoobie's answer works fine. @AndroidNoobie 的回答很好。 I am adding this just in case you want the execution to be async, you could use subprocess.Popen instead of subprocess.run .我添加这个以防万一您希望执行是异步的,您可以使用subprocess.Popen而不是subprocess.run

The UI freezes until the run execution is complete. UI 冻结,直到run执行完成。 If you don't want that to happen, I would recommend using subprocess.Popen如果您不希望这种情况发生,我建议您使用subprocess.Popen

def PING_CLIENT():

    HOST = PING_ENTRY.get()

    command = "ping {} -n 30  -t".format(HOST)
    #subprocess.run(command, shell=True)
    subprocess.Popen(command, shell=True)

From another SO answer : The main difference is that subprocess.run executes a command and waits for it to finish, while with subprocess.Popen you can continue doing your stuff while the process finishes and then just repeatedly call subprocess.communicate yourself to pass and receive data to your process.从另一个 SO 答案:主要区别在于subprocess.run执行一个命令并等待它完成,而使用subprocess.Popen您可以在进程完成时继续做你的事情,然后重复调用 subprocess.communicate 自己通过和接收数据到您的流程。

EDIT: Added code to make the ping stop after 30 trials.编辑:添加代码以在 30 次试验后停止 ping。

To make your code stop after a specific number of packets use the below code.要使您的代码在特定数量的数据包后停止,请使用以下代码。

Windows: Windows:

    command = "ping -n 30 {}".format(HOST)
    pro = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
    print(pro.communicate()[0]) # prints the stdout

Ubuntu: Ubuntu:

    command = "ping -c 30 {}".format(HOST)
    pro = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE)
    print(pro.communicate()[0]) # prints the stdout

-t basically pings indefinitely in windows.That's why you weren't able to stop it. -t 基本上在 windows 中无限期地 ping。这就是你无法阻止它的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在运行 python 可执行文件时出现此错误? - Why am i getting this error when running a python executable? 为什么在运行我的代码时出现错误:'ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required'? - Why am I getting the error: 'ValueError: Found array with 0 sample(s) (shape=(0, 1)) while a minimum of 1 is required' when running my code? 为什么我在运行此脚本时收到 tensorflow 警告? - why am i getting a tensorflow warning when running this script? 运行python脚本时,获取模块未找到错误 - when running python script ,getting module not found error 为什么在我的Python脚本的except子句中出现UnboundLocalError? - Why am I getting an UnboundLocalError in the except clause of my Python script? 当我尝试使用 ngrok 为我的本地主机 5000 为 Twilio 创建 http 隧道时,为什么会出现错误? - Why am I getting an error when I try to use ngrok to create a http tunnel for my local host 5000 for Twilio? 为什么运行此 Function 时出现类型错误? - Why Am I Getting A Type Error When Running This Function? 运行Locust时为什么会出现403错误? - Why am I getting a 403 error when running Locust? 不明白为什么我在 python 脚本中收到此错误 - Not understanding why i am getting this error in python script 当我的 python ddos 脚本运行时出错 - Getting error when my python ddos script running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM