【问题标题】:(Python) gaierror: [Errno 11004] getaddrinfo failed(Python) gaierror: [Errno 11004] getaddrinfo 失败
【发布时间】:2010-11-08 16:52:55
【问题描述】:
from Tkinter import *
import tkMessageBox, socket


root = Tk()
root.title("pynet v1.0")
root.config(bg='black')
root.resizable(0,0)   

text = Text()   
text1 = Text()

text1.config(width=15, height=1)
text1.config(bg="white", fg="red")
text1.pack()

def Info():
    targetip = socket.gethostbyname_ex(text1.get("1.0", END))
    text.insert(END, targetip)

b = Button(root, text="Enter", width=10, height=2, command=Info)
b.config(fg="black", bg="red")
b.pack(side=TOP, padx=5)

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=25, height=5, bg="white", fg="red")
text.pack(side=LEFT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)

root.mainloop()

我正在尝试检索网站的 IP 地址,但在第 18 行不断收到此错误“gaierror: [Errno 11004] getaddrinfo failed”,我们将不胜感激,谢谢。

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Rabia\Desktop\gethostinfo.py", line 18, in Info
    targetip = socket.gethostbyname_ex(text1.get("1.0", END))
gaierror: [Errno 11004] getaddrinfo failed

【问题讨论】:

  • 这样就不用别人算了,第 19 行是targetip = socket.gethostbyname_ex(text1.get("1.0", END) + "\r\n")
  • 你必须摆脱第一个狂野导入from socket import *。它除了会损害性能并弄乱命名空间之外什么也没做。这可能无法解决您的问题

标签: python tkinter


【解决方案1】:

我的猜测是因为您使用的主机名带有尾随换行符。在我写这个答案时,你的代码显示:

def Info():
    targetip = socket.gethostbyname_ex(text1.get("1.0", END))
    text.insert(END, targetip)

当您使用索引END 时,您会得到文本小部件添加的额外换行符。您需要删除它或使用索引"end-1c"

【讨论】:

    【解决方案2】:

    为什么要在查找主机名之前将 CRLF (\r\n) 添加到主机名中?

    如果删除它并不能解决问题,请打印出您传递给 gethostbyname 的确切文本,以确保它是有效的主机名。

    【讨论】:

    • 是的,我忘了删除 (\r\n) 对不起。我只是在随机网站上输入,例如 msn.com、google.com,但没有任何效果。
    • 另外,通过使用索引 END,您将获得一个换行符作为主机名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2018-02-12
    • 2021-10-05
    • 1970-01-01
    相关资源
    最近更新 更多