blob: eaf6699279ba2805a855afedf2598ea417454f06 [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
15instance is initially not connected; the \method{open()} method must
16be used to establish a connection. Alternatively, the host name and
17optional port number can be passed to the constructor, too.
18
19Do not reopen an already connected instance.
20
21This class has many \method{read_*()} methods. Note that some of them
22raise \exception{EOFError} when the end of the connection is read,
23because they can return an empty string for other reasons. See the
Fred Drakeb7168c31999-04-22 17:53:37 +000024individual descriptions below.
Fred Drake658cef01999-03-15 15:44:18 +000025\end{classdesc}
26
27
Fred Drakeac308d02000-04-26 18:20:04 +000028\begin{seealso}
29 \seerfc{854}{Telnet Protocol Specification}{
30 Definition of the Telnet protocol.}
31\end{seealso}
32
33
34
Fred Drake658cef01999-03-15 15:44:18 +000035\subsection{Telnet Objects \label{telnet-objects}}
36
37\class{Telnet} instances have the following methods:
38
39
Fred Drakeb7168c31999-04-22 17:53:37 +000040\begin{methoddesc}{read_until}{expected\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +000041Read until a given string is encountered or until timeout.
42
43When no match is found, return whatever is available instead,
44possibly the empty string. Raise \exception{EOFError} if the connection
45is closed and no cooked data is available.
46\end{methoddesc}
47
Fred Drakeb7168c31999-04-22 17:53:37 +000048\begin{methoddesc}{read_all}{}
49Read all data until \EOF{}; block until connection closed.
Fred Drake658cef01999-03-15 15:44:18 +000050\end{methoddesc}
51
Fred Drakeb7168c31999-04-22 17:53:37 +000052\begin{methoddesc}{read_some}{}
53Read at least one byte of cooked data unless \EOF{} is hit.
54Return \code{''} if \EOF{} is hit. Block if no data is immediately
55available.
Fred Drake658cef01999-03-15 15:44:18 +000056\end{methoddesc}
57
Fred Drakeb7168c31999-04-22 17:53:37 +000058\begin{methoddesc}{read_very_eager}{}
59Read everything that can be without blocking in I/O (eager).
Fred Drake658cef01999-03-15 15:44:18 +000060
61Raise \exception{EOFError} if connection closed and no cooked data
62available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000063Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000064\end{methoddesc}
65
Fred Drakeb7168c31999-04-22 17:53:37 +000066\begin{methoddesc}{read_eager}{}
Fred Drake658cef01999-03-15 15:44:18 +000067Read readily available data.
68
69Raise \exception{EOFError} if connection closed and no cooked data
70available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000071Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000072\end{methoddesc}
73
Fred Drakeb7168c31999-04-22 17:53:37 +000074\begin{methoddesc}{read_lazy}{}
75Process and return data already in the queues (lazy).
Fred Drake658cef01999-03-15 15:44:18 +000076
77Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000078Return \code{''} if no cooked data available otherwise. Do not block
Fred Drake658cef01999-03-15 15:44:18 +000079unless in the midst of an IAC sequence.
80\end{methoddesc}
81
Fred Drakeb7168c31999-04-22 17:53:37 +000082\begin{methoddesc}{read_very_lazy}{}
Fred Drake658cef01999-03-15 15:44:18 +000083Return any data available in the cooked queue (very lazy).
84
85Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000086Return \code{''} if no cooked data available otherwise. This method
87never blocks.
Fred Drake658cef01999-03-15 15:44:18 +000088\end{methoddesc}
89
Fred Drakeb7168c31999-04-22 17:53:37 +000090\begin{methoddesc}{open}{host\optional{, port}}
Fred Drake658cef01999-03-15 15:44:18 +000091Connect to a host.
Fred Drake658cef01999-03-15 15:44:18 +000092The optional second argument is the port number, which
93defaults to the standard telnet port (23).
94
Fred Drakeb7168c31999-04-22 17:53:37 +000095Do not try to reopen an already connected instance.
Fred Drake658cef01999-03-15 15:44:18 +000096\end{methoddesc}
97
Fred Drakeb7168c31999-04-22 17:53:37 +000098\begin{methoddesc}{msg}{msg\optional{, *args}}
99Print a debug message when the debug level is \code{>} 0.
Fred Drake658cef01999-03-15 15:44:18 +0000100If extra arguments are present, they are substituted in the
101message using the standard string formatting operator.
102\end{methoddesc}
103
Fred Drakeb7168c31999-04-22 17:53:37 +0000104\begin{methoddesc}{set_debuglevel}{debuglevel}
105Set the debug level. The higher the value of \var{debuglevel}, the
106more debug output you get (on \code{sys.stdout}).
Fred Drake658cef01999-03-15 15:44:18 +0000107\end{methoddesc}
108
Fred Drakeb7168c31999-04-22 17:53:37 +0000109\begin{methoddesc}{close}{}
Fred Drake658cef01999-03-15 15:44:18 +0000110Close the connection.
111\end{methoddesc}
112
Fred Drakeb7168c31999-04-22 17:53:37 +0000113\begin{methoddesc}{get_socket}{}
Fred Drake658cef01999-03-15 15:44:18 +0000114Return the socket object used internally.
115\end{methoddesc}
116
Fred Drakeb7168c31999-04-22 17:53:37 +0000117\begin{methoddesc}{fileno}{}
118Return the file descriptor of the socket object used internally.
Fred Drake658cef01999-03-15 15:44:18 +0000119\end{methoddesc}
120
Fred Drakeb7168c31999-04-22 17:53:37 +0000121\begin{methoddesc}{write}{buffer}
Fred Drake658cef01999-03-15 15:44:18 +0000122Write a string to the socket, doubling any IAC characters.
Fred Drakeb7168c31999-04-22 17:53:37 +0000123This can block if the connection is blocked. May raise
124\exception{socket.error} if the connection is closed.
Fred Drake658cef01999-03-15 15:44:18 +0000125\end{methoddesc}
126
Fred Drakeb7168c31999-04-22 17:53:37 +0000127\begin{methoddesc}{interact}{}
Fred Drake658cef01999-03-15 15:44:18 +0000128Interaction function, emulates a very dumb telnet client.
129\end{methoddesc}
130
Fred Drakeb7168c31999-04-22 17:53:37 +0000131\begin{methoddesc}{mt_interact}{}
132Multithreaded version of \method{interact()}.
Fred Drake658cef01999-03-15 15:44:18 +0000133\end{methoddesc}
134
Fred Drakeb7168c31999-04-22 17:53:37 +0000135\begin{methoddesc}{expect}{list\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +0000136Read until one from a list of a regular expressions matches.
137
138The first argument is a list of regular expressions, either
139compiled (\class{re.RegexObject} instances) or uncompiled (strings).
Fred Drakeb7168c31999-04-22 17:53:37 +0000140The optional second argument is a timeout, in seconds; the default
141is to block indefinately.
Fred Drake658cef01999-03-15 15:44:18 +0000142
143Return a tuple of three items: the index in the list of the
144first regular expression that matches; the match object
145returned; and the text read up till and including the match.
146
147If end of file is found and no text was read, raise
148\exception{EOFError}. Otherwise, when nothing matches, return
149\code{(-1, None, \var{text})} where \var{text} is the text received so
150far (may be the empty string if a timeout happened).
151
152If a regular expression ends with a greedy match (e.g. \regexp{.*})
153or if more than one expression can match the same input, the
154results are undeterministic, and may depend on the I/O timing.
155\end{methoddesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000156
157
158\subsection{Telnet Example \label{telnet-example}}
159\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
160
161A simple example illustrating typical use:
162
163\begin{verbatim}
164import getpass
165import sys
166import telnetlib
167
168HOST = "localhost"
169user = raw_input("Enter your remote account: ")
170password = getpass.getpass()
171
172tn = telnetlib.Telnet(HOST)
173
174tn.read_until("login: ")
175tn.write(user + "\n")
176if password:
177 tn.read_until("Password: ")
178 tn.write(password + "\n")
179
180tn.write("ls\n")
181tn.write("exit\n")
182
183print tn.read_all()
184\end{verbatim}