Python telnetlib: amazing problem

I am using the telnetlib module for Python to create a telnet session (with a chess server), and I am having a problem that I really cannot wrap around myself. The following code works fine:

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

This spits out everything that the server usually prints at login. However, when I put it inside a function and then call it like this:

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

I get nothing (empty string). I can verify that the login is correct, but for some reason I do not see the text. So where does he swallow?

Many thanks.

For completeness, here is the 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
+3
source share
2 answers

, , , , , , , , . , , .

(, ) :

telnetlib.py(c:\python26\Lib\telnetlib.py Windows) read_very_eager(self) self.sock_avail() sock_avail(self) :

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

, , : -, ( ), True, False.

, read_very_eager(self): , - . , , .

read_some(self), , . , - , , 100 , , 100 , .

+5

, , , select.select, while, , read_some() , 1% . time.sleep(10), read_very_eager(), ... -, . , , , user387821 , .

+3

Source: https://habr.com/ru/post/1770994/


All Articles