Python telnetlib 没有读取所有内容
我正在尝试使用 Python 的 telnetlib 自动下载 Argos 数据,但我似乎不知道如何让它下载所有输出。我的部分问题可能是我并不真正理解命令看似异步的本质。
这是代码:
tn = telnetlib.Telnet(host = HOST, timeout = 60)
with open("argos_prv_{0}-1.txt".format(now_str), 'w') as of:
tn.read_until("Username: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("/")
# Here's the command I'm trying to get the results of:
tn.write("prv,,ds,{0:d},009919,009920\n".format(start_doy))
# At this point, it's presumably dumped it all
tn.read_until("ARGOS READY")
tn.read_until("/")
# Logging out
tn.write("lo\n")
lines = tn.read_all()
of.write(lines)
of.flush()
代码似乎运行得很好,但是当我查看输出文件时,它从来没有包含所有内容,而是在某个随机点被剪切掉。当我在真实的 telnet 会话中输入相同的命令时,它工作得很好。
我感觉这与注销后尝试 read_all()
(tn.write("lo\n")
) 有关,但是当我查看telnetlib 的示例文档,它看起来就像这样。
不管怎样,我的问题是:有人能看到我在这里做错了什么吗?我想获取 prv,,ds 命令的结果,但我只能使用此特定代码获取其中的一些结果。
谢谢。
I'm trying to automate the download of Argos data using Python's telnetlib, but I can't seem to figure out how to get it to download all of the output. Part of my problem may be that I don't really understand the seemingly asynchronous nature of the commands.
Here's the code:
tn = telnetlib.Telnet(host = HOST, timeout = 60)
with open("argos_prv_{0}-1.txt".format(now_str), 'w') as of:
tn.read_until("Username: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
tn.read_until("/")
# Here's the command I'm trying to get the results of:
tn.write("prv,,ds,{0:d},009919,009920\n".format(start_doy))
# At this point, it's presumably dumped it all
tn.read_until("ARGOS READY")
tn.read_until("/")
# Logging out
tn.write("lo\n")
lines = tn.read_all()
of.write(lines)
of.flush()
The code seems to run just fine, but when I look at the output file, it never has everything in it, cutting out at some random point. When I type the same commands in a real telnet session, it works just fine.
I get the sense it has something to do with trying to read_all()
after logging out (tn.write("lo\n")
), but when I look at the example documentation for telnetlib, it pretty much looks just like this.
Anyway, my question is: can anyone see what I'm doing wrong here? I want to grab the results of the prv,,ds
command, but I'm only getting some of it using this particular code.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据猜测,这个位正在吸收数据并且不对其进行任何操作。将其想象为一对管道 - 您使用
write
以一种方式发送内容,并使用read_*
将内容拉回。如果您已经把这些东西吸完了,那么当您稍后执行read_all
时,它就不会仍在管道中等待。编辑:
好吧,你看到了一个不同的问题。试试这个:
At a guess, this bit is sucking up the data and doing nothing with it. Think of it like a pair of pipes - you send stuff one way with
write
, and pull stuff back withread_*
. If you've already sucked the stuff up, it won't still be waiting in the pipe when you doread_all
later.EDIT:
OK, you're seeing a different problem. Try this: