[issue10176] telnetlib.Telnet.read_very_eager() performance

ptz report at bugs.python.org
Tue Oct 26 10:39:54 CEST 2010


ptz <pptzpp at gmail.com> added the comment:

As David suggested, it indeed seems to be a case of timing. When telnetlib.Telnet(...) returns, the server still doesn't have the data cooked, and read_very_eager() fetches nothing. So nothing here fails as such, it's just that my 0 experience in network programming showed.

Either way, some things you could do to get the function g() above to fetch the data the server usually returns upon connection are:

a) insert a time.sleep(t) line after creating the socket. But I don't think there is one answer as to what the value of t should be.
b) if one knows the format of the data that will be received, one could use read_until("expected string")
c) one could write something like

    >>> def g():
    ...   f = telnetlib.Telnet("chessclub.com")
    ...   data = ''
    ...   while not data:
    ...     data = f.read_some()
    ...   print data,f.read_very_eager()
    ...
    >>>

This simply loops until the server has some cooked data available, then fetches it. Tested, works, and is probably the way to do it.

Either way, like David wisely said, this isn't an issue with either Python or telnetlib. However, it may be a good idea to add a warning to the documentation to the effect that by the time the Telnet constructor returns cooked data is typically not yet available from the server. To a beginner network programmer, this is far from obvious.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10176>
_______________________________________


More information about the Python-bugs-list mailing list