我正在尝试使用gethostbyname为网站制作IP解析器,这是我得到的错误:
File "wexec.py", line 39, in hell
ipname = socket.gethostbyname('http://%s') % (hcon)
socket.gaierror: [Errno 11004] getaddrinfo failed
我试图解决它,我尝试放('www.youtube.com'),然后它工作。我不确定我做错了什么。但这是我的代码:
def hell():
hcon = raw_input(Fore.RED + Style.BRIGHT + "Website: ")
urlopen = urllib2.urlopen('http://%s:80' % (hcon))
ipname = socket.gethostbyname('http://%s') % (hcon)
print(strftime("[%H:%M:%S]", gmtime()) + " Found IP: %s " % (ipname))
enter = raw_input("Press enter or any other key to continue.")
hell()
那我该怎么办?有人可以帮忙吗?
答案 0 :(得分:0)
这一行
ipname = socket.gethostbyname('http://%s') % (hcon)
您已将"http://%s"
传递给gethostbyname
试试这个:
ipname = socket.gethostbyname('http://%s' % hcon)
答案 1 :(得分:0)
首先,您的包围略微偏离。而不是:
ipname = socket.gethostbyname('http://%s') % (hcon)
它应该是:
ipname = socket.gethostbyname('http://%s' % (hcon))
通过尽早结束对gethostbyname
的调用,您阻止了%s
字符串格式化,这意味着该名称包含文字 %s
而不是hcon
的价值。
我也不确定您是否应该在主机名中加入http://
。主机名与URL不同。您可能希望从前面删除URL类型,在这种情况下,您可以将其简化为:
ipname = socket.gethostbyname(hcon)