Aloha,我正在创建一个teamspeakbot并希望编写和读取使用TeamSpeak3服务器创建的telnet会话
import telnetlib
tn = telnetlib.Telnet('localhost', 10011, 10)
tn.read_all()
我期待的是:
Connected to localhost
Escape character is '^]'.
TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a li...
但相反,我在10秒后得到一个时间:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/telnetlib.py", line 325, in read_all
self.fill_rawq()
File "/usr/lib/python2.7/telnetlib.py", line 516, in fill_rawq
buf = self.sock.recv(50)
socket.timeout: timed out
我如何阅读telnet连接告诉我的所有内容,然后写一些内容(如登录过程和提交命令并获得响应......)
tn.read_very_eager()
我的代码现在看起来像这样:
import telnetlib, time
tn = telnetlib.Telnet('localhost', 10011, 10)
tn.write('help\n')
time.sleep(0.05)
print(tn.read_very_eager())
答案 0 :(得分:0)
read_all()阻塞,直到达到EOF *,或者直到达到超时。虽然我没有太多地使用telnetlib,但我认为它是用于显示某些内容然后关闭连接的服务。
如何使用以下代码:
tn = telnetlib.Telnet('localhost', 10011, 10)
tn.read_some()
* https://docs.python.org/2/library/telnetlib.html#telnetlib.Telnet.read_all