0

我正在尝试通过 python 创建一个远程登录到 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

我究竟做错了什么

4

1 回答 1

0

For each character in HOST, you are attempting to resolve that character; this is due to your use of a for loop in combination with indexing HOST. For example in the first iteration you attempt to connect to a host with an I.P. of "1"- that is what produces the gaierror.

To fix it, just don't attempt to connect to a specific index of HOST: get rid of HOST[i] and replace it with HOST alone in the call to telnetlib.Telnet:

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

As to what you're trying to do with that loop, however, I'm baffled.

于 2014-10-27T21:50:40.407 回答