-->

Python - Telnet closes before waiting for a print

2019-08-26 08:33发布

问题:

I am making a python script that conects to Teamspeak 3 Server Query telnet and listens out for commands (I haven't figured out the command part yet). But right now the telnet doesn't wait till a response is made, should i be using another library or is there a telnet object I can use to keep it open? Code:

__author__ = 'Khailz'

import telnetlib, socket, time, ConfigParser, time

#login Credidentials
config = ConfigParser.ConfigParser()
config.readfp(open(r'info.conf'))
username = config.get('File', 'username')
password = config.get('File', 'password')
host = config.get('File', 'host')
port = config.get('File', 'port')


timeout = 30


def connect_server(host, port):
    try:
        tn = telnetlib.Telnet(host, port, timeout)
    except socket.timeout:
        print ("socket timeout")
    else:
        tn.write("login %s %s\n" % (username, password))
        tn.write("use 1\n")
        tn.write("clientupdate client_nickname=Admin\n")
        tn.write("clientpoke clid=2 msg=Connected\n")
        tn.write("servernotifyregister event=textprivate\n")
        tn.write("gm msg=Bot\sConnected!\n")
        tn.read_until("notifytextmessage targetmode=1 msg=", timeout=9999)


connect_server(host, port)