Python telnetlib 没有读取所有内容

发布于 2024-11-14 04:39:22 字数 1011 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2024-11-21 04:39:22
# At this point, it's presumably dumped it all
tn.read_until("ARGOS READY")
tn.read_until("/")

据猜测,这个位正在吸收数据并且不对其进行任何操作。将其想象为一对管道 - 您使用 write 以一种方式发送内容,并使用 read_* 将内容拉回。如果您已经把这些东西吸完了,那么当您稍后执行 read_all 时,它就不会仍在管道中等待。

编辑:
好吧,你看到了一个不同的问题。试试这个:

lines = tn.read_until("ARGOS READY")
lines += tn.read_until("/")
tn.write("lo\n")
# Write out lines to file.
# At this point, it's presumably dumped it all
tn.read_until("ARGOS READY")
tn.read_until("/")

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 with read_*. If you've already sucked the stuff up, it won't still be waiting in the pipe when you do read_all later.

EDIT:
OK, you're seeing a different problem. Try this:

lines = tn.read_until("ARGOS READY")
lines += tn.read_until("/")
tn.write("lo\n")
# Write out lines to file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文