Python:gethostbyname错误

时间:2013-07-31 19:22:36

标签: python ip urllib strftime httplib

我在python中编写了一个简单的程序,可以让你找到URL的IP。我收到这个错误:

File "wexec.py", line 40, in hell
  ipname = socket.gethostbyname('http://%s' % (hcon))
socket.gaierror: [Errno 11004] getaddrinfo failed

现在我不确定我做错了什么,但这是我的功能代码:

def hell():

    hcon = raw_input(Fore.RED + Style.BRIGHT + "Website: ")
    h1 = httplib.HTTPConnection('http://%s:80' % (hcon))
    urlopen = urllib.urlopen('http://%s:80' % (hcon))   
    ipname = socket.gethostbyname('http://%s' % (hcon))
    print(strftime("[%H:%M:%S]", gmtime()) + " Found IP: %d " % (ipname))
    enter = raw_input("Press enter or any other key to continue.")

hell()

如你所见。我打开一个到网站的HTTP连接,然后我用urllibb打开URL,然后我得到了网站的IP。但正如你所看到的,我不确定我做错了什么。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

socket.gethostbyname()只接受域名,因此您需要删除' http://'从那个电话。我不明白对httplib和urllib的调用是什么,但是从这段代码中看起来没什么用。