blob: 50d0c539038279360d3271d56f3c51a7e5a9bb21 [file] [log] [blame]
Fred Drake658cef01999-03-15 15:44:18 +00001\section{\module{telnetlib} ---
2 Telnet client}
3
4\declaremodule{standard}{telnetlib}
5\modulesynopsis{Telnet client class.}
Fred Drake38e5d272000-04-03 20:13:55 +00006\sectionauthor{Skip Montanaro}{skip@mojam.com}
Fred Drake658cef01999-03-15 15:44:18 +00007
8The \module{telnetlib} module provides a \class{Telnet} class that
9implements the Telnet protocol. See \rfc{854} for details about the
10protocol.
11
12
Fred Drakeb7168c31999-04-22 17:53:37 +000013\begin{classdesc}{Telnet}{\optional{host\optional{, port}}}
Fred Drake658cef01999-03-15 15:44:18 +000014\class{Telnet} represents a connection to a telnet server. The
Fred Drakeae088532000-05-03 15:11:47 +000015instance is initially not connected by default; the \method{open()}
16method must be used to establish a connection. Alternatively, the
17host name and optional port number can be passed to the constructor,
18to, in which case the connection to the server will be established
19before the constructor returns.
Fred Drake658cef01999-03-15 15:44:18 +000020
21Do not reopen an already connected instance.
22
23This class has many \method{read_*()} methods. Note that some of them
24raise \exception{EOFError} when the end of the connection is read,
25because they can return an empty string for other reasons. See the
Fred Drakeb7168c31999-04-22 17:53:37 +000026individual descriptions below.
Fred Drake658cef01999-03-15 15:44:18 +000027\end{classdesc}
28
29
Fred Drakeac308d02000-04-26 18:20:04 +000030\begin{seealso}
31 \seerfc{854}{Telnet Protocol Specification}{
32 Definition of the Telnet protocol.}
33\end{seealso}
34
35
36
Fred Drake658cef01999-03-15 15:44:18 +000037\subsection{Telnet Objects \label{telnet-objects}}
38
39\class{Telnet} instances have the following methods:
40
41
Fred Drakeb7168c31999-04-22 17:53:37 +000042\begin{methoddesc}{read_until}{expected\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +000043Read until a given string is encountered or until timeout.
44
45When no match is found, return whatever is available instead,
46possibly the empty string. Raise \exception{EOFError} if the connection
47is closed and no cooked data is available.
48\end{methoddesc}
49
Fred Drakeb7168c31999-04-22 17:53:37 +000050\begin{methoddesc}{read_all}{}
51Read all data until \EOF{}; block until connection closed.
Fred Drake658cef01999-03-15 15:44:18 +000052\end{methoddesc}
53
Fred Drakeb7168c31999-04-22 17:53:37 +000054\begin{methoddesc}{read_some}{}
55Read at least one byte of cooked data unless \EOF{} is hit.
56Return \code{''} if \EOF{} is hit. Block if no data is immediately
57available.
Fred Drake658cef01999-03-15 15:44:18 +000058\end{methoddesc}
59
Fred Drakeb7168c31999-04-22 17:53:37 +000060\begin{methoddesc}{read_very_eager}{}
61Read everything that can be without blocking in I/O (eager).
Fred Drake658cef01999-03-15 15:44:18 +000062
63Raise \exception{EOFError} if connection closed and no cooked data
64available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000065Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000066\end{methoddesc}
67
Fred Drakeb7168c31999-04-22 17:53:37 +000068\begin{methoddesc}{read_eager}{}
Fred Drake658cef01999-03-15 15:44:18 +000069Read readily available data.
70
71Raise \exception{EOFError} if connection closed and no cooked data
72available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000073Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000074\end{methoddesc}
75
Fred Drakeb7168c31999-04-22 17:53:37 +000076\begin{methoddesc}{read_lazy}{}
77Process and return data already in the queues (lazy).
Fred Drake658cef01999-03-15 15:44:18 +000078
79Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000080Return \code{''} if no cooked data available otherwise. Do not block
Fred Drake658cef01999-03-15 15:44:18 +000081unless in the midst of an IAC sequence.
82\end{methoddesc}
83
Fred Drakeb7168c31999-04-22 17:53:37 +000084\begin{methoddesc}{read_very_lazy}{}
Fred Drake658cef01999-03-15 15:44:18 +000085Return any data available in the cooked queue (very lazy).
86
87Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000088Return \code{''} if no cooked data available otherwise. This method
89never blocks.
Fred Drake658cef01999-03-15 15:44:18 +000090\end{methoddesc}
91
Fred Drakeb7168c31999-04-22 17:53:37 +000092\begin{methoddesc}{open}{host\optional{, port}}
Fred Drake658cef01999-03-15 15:44:18 +000093Connect to a host.
Fred Drake658cef01999-03-15 15:44:18 +000094The optional second argument is the port number, which
95defaults to the standard telnet port (23).
96
Fred Drakeb7168c31999-04-22 17:53:37 +000097Do not try to reopen an already connected instance.
Fred Drake658cef01999-03-15 15:44:18 +000098\end{methoddesc}
99
Fred Drakeb7168c31999-04-22 17:53:37 +0000100\begin{methoddesc}{msg}{msg\optional{, *args}}
101Print a debug message when the debug level is \code{>} 0.
Fred Drake658cef01999-03-15 15:44:18 +0000102If extra arguments are present, they are substituted in the
103message using the standard string formatting operator.
104\end{methoddesc}
105
Fred Drakeb7168c31999-04-22 17:53:37 +0000106\begin{methoddesc}{set_debuglevel}{debuglevel}
107Set the debug level. The higher the value of \var{debuglevel}, the
108more debug output you get (on \code{sys.stdout}).
Fred Drake658cef01999-03-15 15:44:18 +0000109\end{methoddesc}
110
Fred Drakeb7168c31999-04-22 17:53:37 +0000111\begin{methoddesc}{close}{}
Fred Drake658cef01999-03-15 15:44:18 +0000112Close the connection.
113\end{methoddesc}
114
Fred Drakeb7168c31999-04-22 17:53:37 +0000115\begin{methoddesc}{get_socket}{}
Fred Drake658cef01999-03-15 15:44:18 +0000116Return the socket object used internally.
117\end{methoddesc}
118
Fred Drakeb7168c31999-04-22 17:53:37 +0000119\begin{methoddesc}{fileno}{}
120Return the file descriptor of the socket object used internally.
Fred Drake658cef01999-03-15 15:44:18 +0000121\end{methoddesc}
122
Fred Drakeb7168c31999-04-22 17:53:37 +0000123\begin{methoddesc}{write}{buffer}
Fred Drake658cef01999-03-15 15:44:18 +0000124Write a string to the socket, doubling any IAC characters.
Fred Drakeb7168c31999-04-22 17:53:37 +0000125This can block if the connection is blocked. May raise
126\exception{socket.error} if the connection is closed.
Fred Drake658cef01999-03-15 15:44:18 +0000127\end{methoddesc}
128
Fred Drakeb7168c31999-04-22 17:53:37 +0000129\begin{methoddesc}{interact}{}
Fred Drake658cef01999-03-15 15:44:18 +0000130Interaction function, emulates a very dumb telnet client.
131\end{methoddesc}
132
Fred Drakeb7168c31999-04-22 17:53:37 +0000133\begin{methoddesc}{mt_interact}{}
134Multithreaded version of \method{interact()}.
Fred Drake658cef01999-03-15 15:44:18 +0000135\end{methoddesc}
136
Fred Drakeb7168c31999-04-22 17:53:37 +0000137\begin{methoddesc}{expect}{list\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +0000138Read until one from a list of a regular expressions matches.
139
140The first argument is a list of regular expressions, either
141compiled (\class{re.RegexObject} instances) or uncompiled (strings).
Fred Drakeb7168c31999-04-22 17:53:37 +0000142The optional second argument is a timeout, in seconds; the default
Thomas Woutersf8316632000-07-16 19:01:10 +0000143is to block indefinitely.
Fred Drake658cef01999-03-15 15:44:18 +0000144
145Return a tuple of three items: the index in the list of the
146first regular expression that matches; the match object
147returned; and the text read up till and including the match.
148
149If end of file is found and no text was read, raise
150\exception{EOFError}. Otherwise, when nothing matches, return
151\code{(-1, None, \var{text})} where \var{text} is the text received so
152far (may be the empty string if a timeout happened).
153
154If a regular expression ends with a greedy match (e.g. \regexp{.*})
155or if more than one expression can match the same input, the
Thomas Woutersf8316632000-07-16 19:01:10 +0000156results are indeterministic, and may depend on the I/O timing.
Fred Drake658cef01999-03-15 15:44:18 +0000157\end{methoddesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000158
159
160\subsection{Telnet Example \label{telnet-example}}
161\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
162
163A simple example illustrating typical use:
164
165\begin{verbatim}
166import getpass
167import sys
168import telnetlib
169
170HOST = "localhost"
171user = raw_input("Enter your remote account: ")
172password = getpass.getpass()
173
174tn = telnetlib.Telnet(HOST)
175
176tn.read_until("login: ")
177tn.write(user + "\n")
178if password:
179 tn.read_until("Password: ")
180 tn.write(password + "\n")
181
182tn.write("ls\n")
183tn.write("exit\n")
184
185print tn.read_all()
186\end{verbatim}