Python telnetlib:令人惊讶的问题 [英] Python telnetlib: surprising problem

查看:172
本文介绍了Python telnetlib:令人惊讶的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python模块telnetlib创建一个telnet会话(使用国际象棋服务器),但遇到了一个问题,我确实无法解决.以下代码可以完美运行:

I am using the Python module telnetlib to create a telnet session (with a chess server), and I'm having an issue I really can't wrap my brain around. The following code works perfectly:

>>> f = login("my_server") #code for login(host) below.
>>> f.read_very_eager()

这将散出服务器通常在登录时打印的所有内容.但是,当我将其放在函数中然后按以下方式调用时:

This spits out everything the server usually prints upon login. However, when I put it inside a function and then call it thus:

>>> def foo():
...   f = login("my_server")
...   return f.read_very_eager()
...
>>> foo()

我什么也没得到(空字符串).我可以检查登录是否正确执行,但是由于某种原因我看不到文本.那么它在哪里被吞下?

I get nothing (the empty string). I can check that the login is performed properly, but for some reason I can't see the text. So where does it get swallowed?

非常感谢.

为完整起见,这是登录名(主机):

For completeness, here is login(host):

def login(host, handle="guest", password=""):
try:
    f = telnetlib.Telnet(host) #connect to host
except:
    raise Error("Could not connect to host")
f.read_until("login: ")
try:
    f.write(handle + "\n\r")
except:
    raise Error("Could not write username to host")
if handle == "guest":
    f.read_until(":\n\r")
else:
    f.read_until("password: ")
try:
    f.write(password + "\n\r")
except:
    raise Error("Could not write password to host")
return f

推荐答案

在手动尝试时起作用但在功能中不能起作用的原因是,当手动尝试时,服务器有足够的时间做出反应登录后将数据发送回去.当它具有全部功能时,您便将密码发送到服务器,而不必等待足够长的时间才能让服务器回复.

The reason why this works when you try it out manually but not when in a function is because when you try it out manually, the server has enough time to react upon the login and send data back. When it's all in one function, you send the password to the server and never wait long enough for the server to reply.

如果您希望使用(可能更正确的)技术答案:

If you prefer a (probably more correct) technical answer:

在文件telnetlib.py(在我的Windows计算机上为c:\ python26 \ Lib \ telnetlib.py)中,函数read_very_eager(self)调用self.sock_avail()现在,函数sock_avail(self)执行以下操作:

In file telnetlib.py (c:\python26\Lib\telnetlib.py on my Windows computer), function read_very_eager(self) calls self.sock_avail() Now, function sock_avail(self) does the following:

def sock_avail(self):
    """Test whether data is available on the socket."""
    return select.select([self], [], [], 0) == ([self], [], [])

这确实很简单:如果有任何东西要从我们的套接字读取(服务器已回答),它将返回True,否则将返回False.

What this does is really simple: if there is -anything- to read from our socket (the server has answered), it'll return True, otherwise it'll return False.

因此,read_very_eager(self)的作用是:检查是否有任何可读取的内容.如果存在,则从套接字读取,否则只需返回一个空字符串.

So, what read_very_eager(self) does is: check if there is anything available to read. If there is, then read from the socket, otherwise just return an empty string.

如果查看read_some(self)的代码,您会发现它不检查是否有任何可读取的数据.它会尝试阅读直到有可用的东西为止,这意味着如果服务器在回答您的问题之前花费了100毫秒,那么它将等待100毫秒才返回答案.

If you look at the code of read_some(self) you'll see that it doesn't check if there is any data available to read. It'll try reading till there is something available, which means that if the server takes for instance 100ms before answering you, it'll wait 100ms before returning the answer.

这篇关于Python telnetlib:令人惊讶的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆