18.5 Telnet
Telnet is an old protocol, specified by
RFC 854 (see http://www.faqs.org/rfcs/rfc854.html), and
normally used for interactive user sessions. The Python standard
library supports this protocol in its module
telnetlib. Module telnetlib
supplies a class Telnet to connect to a Telnet
server.
class Telnet(host=None,port=23)
|
|
Returns an instance t of class
Telnet. When host (and
optionally port) is given, implicitly
calls
t.open(host,port).
Instance t supplies many methods, of which
the most frequently used are as follows.
Closes the connection.
t.expect(res,timeout=None)
|
|
Reads data from the connection until it matches any of the regular
expressions that are the items of list
res, or until
timeout seconds elapse when
timeout is not None.
Regular expressions and match objects are covered in Chapter 9. Returns a tuple of three items
(i,mo,txt),
where i is the index in
res of the regular expression that
matched, mo is the match object, and
txt is all the text read until the match,
included. Raises EOFError when the connection is
closed and no data is available; otherwise, when it gets no match,
returns
(-1,None,txt),
where txt is all the text read, or
possibly '' if nothing was read before a timeout.
Results are non-deterministic if more than one item in
res can match, or if any of the items in
res include greedy parts (such as
'.*').
Enters interactive mode, connecting
standard input and output to the two channels of the connection, like
a dumb Telnet client.
Connects to a Telnet server on the
given host and
port. Call once per instance
t, as
t's first method call.
Don't call if host was
given on creation.
Reads data from the connection until
the connection is closed, then returns all available data. Blocks
until the time the connection is closed.
Reads and returns everything that can
be read from the connection without blocking; may be the empty string
''. Raises EOFError if the
connection is closed and no data is available.
Reads and returns at least one byte of data from the connection,
unless the connection is closed, in which case it returns
''. Blocks until at least one byte of data is
available.
t.read_until(expected,timeout=None)
|
|
Reads data from the connection until it
encounters string expected, or until
timeout seconds elapse when
timeout is not None.
Returns whatever data is available at that time, or possibly the
empty string ''. Raises
EOFError if the connection is closed and no data
is available.
Writes string astring to the connection.
|