views:

36

answers:

0

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?

Many thanks.

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