简体   繁体   中英

(Python) socket.gaierror: [Errno 11001] getaddrinfo failed

I'm not sure whats wrong with this code I keep getting that socket.gaierror error ;\\ .

import sys
import socket
import random

filename = "whoiservers.txt"

server_name = random.choice(list(open(filename)))

print "connecting to %s..." % server_name

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server_name, 43))
s.send(sys.argv[1] + "\r\n")
response = ''
while True:
    d = s.recv(4096)
    response += d
    if d == '':
        break
s.close()
print
print response


    s.connect((server_name, 43))
  File "<string>", line 1, in connect
socket.gaierror: [Errno 11001] getaddrinfo failed

Update:

After adding server_name = random.choice(list(open(filename)))[:-1] I dont get that socket.gaierror anymore but I get:

socket.error: [Errno 10060] A connection attempt failed because the connected pa rty did not properly respond after a period of time, or established connection f ailed because connected host has failed to respond

I think the problem is a newline at the end of server_name .

If the format of your file whoiservers.txt is one hostname on each line then you need to strip the newline at the end of the hostname before passing it to s.connect()

So, for example, change the open line to:

server_name = random.choice(list(open(filename)))[:-1]

Perhaps you have a firewall in between you and these servers that is blocking the request? The last error you posted leads one to believe that it cannot connect to the server at all...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM