Python - Telnet closes before waiting for a print read function

六眼飞鱼酱① 提交于 2019-12-12 04:34:22

问题


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)

来源:https://stackoverflow.com/questions/26599762/python-telnet-closes-before-waiting-for-a-print-read-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!