Python - Telnetlib socket.gaierror:

时间:2014-10-27 21:43:43

标签: python sockets telnetlib

我正在尝试通过python创建一个telnet到Teamspeak 3服务器查询,这是我的代码:

__author__ = 'Khailz'

import telnetlib, socket

HOST = "unlightedgaming.com"
port = 10011
timeout = 5



for i in range(len(HOST)):
    print "scanning " + HOST[i] + " ...\n"
    try:
        tn = telnetlib.Telnet(HOST[i],23,3)
    except socket.timeout:
        pass

#tn = telnetlib.Telnet(host, port, timeout)
#telnetlib.Telnet(host, port, timeout)

tn.read_until("Welcome to the TeamSpeak 3 ServerQuery interface")

print tn.read_all()

但我似乎从套接字中得到错误,说它无法获取地址。

C:\Python27\python.exe C:/Users/Khailz/PycharmProjects/Teamspeak-IRC/test.py
scanning 1 ...

Traceback (most recent call last):
  File "C:/Users/KhailzXsniper/PycharmProjects/Teamspeak-IRC/test.py", line 14, in <module>
    tn = telnetlib.Telnet(HOST[i],23,3)
  File "C:\Python27\lib\telnetlib.py", line 211, in __init__
    self.open(host, port, timeout)
  File "C:\Python27\lib\telnetlib.py", line 227, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed

我做错了什么

1 个答案:

答案 0 :(得分:0)

对于HOST中的每个字符,您正试图解析该字符;这是因为您将for循环与索引HOST结合使用。例如,在第一次迭代中,您尝试使用I.P.连接到主机。 "1" - 这就是产生gaierror的原因。

要解决此问题,请不要尝试连接到HOST的特定索引:摆脱HOST[i]并在调用{HOST时单独替换telnetlib.Telnet {1}}:

tn = telnetlib.Telnet(HOST, 23, 3)

然而,至于你试图用这个循环做什么,我感到很困惑。