Section 19.5. Telnet


19.5. Telnet

Telnet is an old protocol, specified by RFC 854 (see http://www.ietf.org/rfc/rfc854.txt), and is 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.

Telnet

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.

close

t.close( )

Closes the connection.

expect

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 "Regular Expressions and the re Module" on page 201.) 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 nondeterministic if more than one item in res can match, or if any of the items in res include greedy parts (such as '.*').

interact

t.interact( )

Enters interactive mode, connecting standard input and output to the two channels of the connection, like a dumb Telnet client.

open

t.open(host,port=23)

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.

read_all

t.read_all( )

Reads data from the connection until the connection is closed, then returns all available data. Blocks until the connection is closed.

read_eager

t.read_eager( )

Reads and returns all 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.

read_some

t.read_some( )

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.

read_until

t.read_until(expected,timeout=None)

Reads data from the connection until it finds string expected, or timeout seconds elapse when timeout is not None. Returns the data available at that time, possibly the empty string ''. Raises EOFError if the connection is closed and no data is available.

write

t.write(astring)

Writes string astring to the connection.





Python in a Nutshell
Python in a Nutshell, Second Edition (In a Nutshell)
ISBN: 0596100469
EAN: 2147483647
Year: 2004
Pages: 192
Authors: Alex Martelli

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net