gaierror:提供了[Errno 8] nodename或servname,或者未知

时间:2016-07-26 06:56:32

标签: python sockets

您好我是python的新手,我正在编写一个生成dns请求的代码

    from socket import error as socket_error
    import threading
    from random import randint
    from time import sleep

    def task(number):
       try :
          HOST = Random_website.random_website().rstrip() # fetches url
          PORT = 80              # The same port as used by the server
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

          s.connect((HOST, PORT))
          print(str(number) +":"+HOST +"Connected")
       except socket_error as serr:
         if serr.errno != errno.ECONNREFUSED:
           # Not the error we are looking for, re-raise
            raise serr      

  thread_list = []

  for i in range(1, 100):    
    t = threading.Thread(target=task, args=(i,))
    thread_list.append(t)

  for thread in thread_list:
      thread.start()

执行上面的代码会抛出此错误,任何人都可以帮助我摆脱这个错误 我从一天起拔出头发

提前致谢

2 个答案:

答案 0 :(得分:2)

喜欢这样:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(("www.google.com",80))
>>> s.connect(("http://www.google.com",80))

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    s.connect(("http://www.google.com",80))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
gaierror: [Errno -2] Name or service not known
>>> 

套接字不是HTTP连接!

在发送请求之前删除HTTP://代码!

编辑:

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(("digan.net",80))
>>> s.connect(("digan.net/hahaha/hihihi/etc",80))

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    s.connect(("digan.net/hahaha/hihihi/etc",80))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
gaierror: [Errno -2] Name or service not known
>>> 

套接字无法向其他路径发送请求。只与服务器通话!

答案 1 :(得分:0)

当Web代理处于“打开”状态时,我收到了相同的错误消息,但该URL指向本地网络中的计算机,无需代理即可访问。将web-proxy(http)设置为“off”为我修复了它。

希望这有帮助,Alex。