【发布时间】:2016-08-17 23:14:47
【问题描述】:
我正在编写一个 Python 脚本来解析来自远程 telnet 服务器端口 10001 上的一些数据。基本上,当我键入时:
$ telnet <host> 10001
终端打印出来:
Trying <host>...
Connected to static-<host>.nycmny.fios.verizon.net.
Escape character is '^]'.
# empty line for prompt
在注释的空行中,我应该输入如下命令('\n' 代表回车):
^Ai20101\n
# server prints out data
somedatalinehere
^]
# escape to telnet prompt like below
telnet>
telnet> quit\n
connection closed.
# returns to local terminal prompt
但是,当我在 Python 中执行此操作时:
tn = telnetlib.Telnet(host, 10001)
tn.read_until("\r\n", timeout=1) # nothing matched, returns ''
tn.read_until("", timeout=1) # nothing matched, returns ''
# thus
tn.write("^Ai20101\n")
time.sleep(0.1) # wait 0.1s for next prompt
tn.write("^]")
time.sleep(0.1)
tn.write("quit\n")
tn.read_all() # This hangs as if connection wasn't closed.
【问题讨论】: