开发者

(Python) gaierror: [Errno 11004] getaddrinfo failed

开发者 https://www.devze.com 2023-01-23 20:57 出处:网络
from Tkinter import * import tkMessageBox, socket root = Tk() root.title(\"pynet v1.0\") root.config(bg=\'black\')
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()

I'm trying to retrieve the IP Address of a website, but I keep getting this error, "gaierror: [Errno 11004]开发者_开发百科 getaddrinfo failed" in line 18, your help will be appreciated, Thanks.

The Error:

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


My guess is because you are using a hostname that has a trailing newline. At the time I write this answer, your code shows:

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

When you use the index END you get the extra newline that is added by the text widget. You need to strip that off or use the index "end-1c".


Why are you adding CRLF (\r\n) to the hostname before looking it up?

If removing that doesn't fix it, print out the exact text you're passing to gethostbyname to make sure it's a valid hostname.

0

精彩评论

暂无评论...
验证码 换一张
取 消