Search code examples
pythonsocketstelnetlib

Python - Telnetlib socket.gaierror:


I am trying to create a telnet to Teamspeak 3 Server Query through python, this is my code:

__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()

But i seem to get the error from socket saying that it cannot get the address.

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

What am I doing wrong


Solution

  • 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.