blob: 853788f2a9b5bfd797079f21109b79f924d98602 [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
Fred Drakeab1df4f2001-07-06 20:23:02 +00008\index{protocol!Telnet}
9
Fred Drake658cef01999-03-15 15:44:18 +000010The \module{telnetlib} module provides a \class{Telnet} class that
11implements the Telnet protocol. See \rfc{854} for details about the
Martin v. Löwisb0162f92001-09-06 08:51:38 +000012protocol. In addition, it provides symbolic constants for the protocol
Martin v. Löwis22610da2002-11-04 17:41:18 +000013characters (see below), and for the telnet options. The
Martin v. Löwisb0162f92001-09-06 08:51:38 +000014symbolic names of the telnet options follow the definitions in
15\code{arpa/telnet.h}, with the leading \code{TELOPT_} removed. For
16symbolic names of options which are traditionally not included in
17\code{arpa/telnet.h}, see the module source itself.
Fred Drake658cef01999-03-15 15:44:18 +000018
Martin v. Löwis22610da2002-11-04 17:41:18 +000019The symbolic constants for the telnet commands are: IAC, DONT, DO,
20WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data
21Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT
22(Are You There), EC (Erase Character), EL (Erase Line), GA (Go Ahead),
23SB (Subnegotiation Begin).
24
Fred Drake658cef01999-03-15 15:44:18 +000025
Guido van Rossumd8faa362007-04-27 19:54:29 +000026\begin{classdesc}{Telnet}{\optional{host\optional{, port\optional{, timeout}}}}
Fred Drakeab1df4f2001-07-06 20:23:02 +000027\class{Telnet} represents a connection to a Telnet server. The
Fred Drakeae088532000-05-03 15:11:47 +000028instance is initially not connected by default; the \method{open()}
29method must be used to establish a connection. Alternatively, the
30host name and optional port number can be passed to the constructor,
31to, in which case the connection to the server will be established
32before the constructor returns.
Guido van Rossumd8faa362007-04-27 19:54:29 +000033The optional \var{timeout} parameter specifies a timeout in seconds for the
34connection attempt (if not specified, or passed as None, the global default
35timeout setting will be used).
Fred Drake658cef01999-03-15 15:44:18 +000036
37Do not reopen an already connected instance.
38
39This class has many \method{read_*()} methods. Note that some of them
40raise \exception{EOFError} when the end of the connection is read,
41because they can return an empty string for other reasons. See the
Fred Drakeb7168c31999-04-22 17:53:37 +000042individual descriptions below.
Guido van Rossumcd16bf62007-06-13 18:07:49 +000043\versionchanged[\var{timeout} was added]{2.6}
Fred Drake658cef01999-03-15 15:44:18 +000044\end{classdesc}
45
46
Fred Drakeac308d02000-04-26 18:20:04 +000047\begin{seealso}
48 \seerfc{854}{Telnet Protocol Specification}{
49 Definition of the Telnet protocol.}
50\end{seealso}
51
52
53
Fred Drake658cef01999-03-15 15:44:18 +000054\subsection{Telnet Objects \label{telnet-objects}}
55
56\class{Telnet} instances have the following methods:
57
58
Guido van Rossumd8faa362007-04-27 19:54:29 +000059\begin{methoddesc}[Telnet]{read_until}{expected\optional{, timeout}}
Fred Drake83c19ee2003-04-29 13:39:05 +000060Read until a given string, \var{expected}, is encountered or until
61\var{timeout} seconds have passed.
Fred Drake658cef01999-03-15 15:44:18 +000062
63When no match is found, return whatever is available instead,
64possibly the empty string. Raise \exception{EOFError} if the connection
65is closed and no cooked data is available.
66\end{methoddesc}
67
Guido van Rossumd8faa362007-04-27 19:54:29 +000068\begin{methoddesc}[Telnet]{read_all}{}
Fred Drakec37b65e2001-11-28 07:26:15 +000069Read all data until \EOF; block until connection closed.
Fred Drake658cef01999-03-15 15:44:18 +000070\end{methoddesc}
71
Guido van Rossumd8faa362007-04-27 19:54:29 +000072\begin{methoddesc}[Telnet]{read_some}{}
Fred Drakeb7168c31999-04-22 17:53:37 +000073Read at least one byte of cooked data unless \EOF{} is hit.
74Return \code{''} if \EOF{} is hit. Block if no data is immediately
75available.
Fred Drake658cef01999-03-15 15:44:18 +000076\end{methoddesc}
77
Guido van Rossumd8faa362007-04-27 19:54:29 +000078\begin{methoddesc}[Telnet]{read_very_eager}{}
Fred Drakeb7168c31999-04-22 17:53:37 +000079Read everything that can be without blocking in I/O (eager).
Fred Drake658cef01999-03-15 15:44:18 +000080
81Raise \exception{EOFError} if connection closed and no cooked data
82available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000083Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000084\end{methoddesc}
85
Guido van Rossumd8faa362007-04-27 19:54:29 +000086\begin{methoddesc}[Telnet]{read_eager}{}
Fred Drake658cef01999-03-15 15:44:18 +000087Read readily available data.
88
89Raise \exception{EOFError} if connection closed and no cooked data
90available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000091Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000092\end{methoddesc}
93
Guido van Rossumd8faa362007-04-27 19:54:29 +000094\begin{methoddesc}[Telnet]{read_lazy}{}
Fred Drakeb7168c31999-04-22 17:53:37 +000095Process and return data already in the queues (lazy).
Fred Drake658cef01999-03-15 15:44:18 +000096
97Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000098Return \code{''} if no cooked data available otherwise. Do not block
Fred Drake658cef01999-03-15 15:44:18 +000099unless in the midst of an IAC sequence.
100\end{methoddesc}
101
Guido van Rossumd8faa362007-04-27 19:54:29 +0000102\begin{methoddesc}[Telnet]{read_very_lazy}{}
Fred Drake658cef01999-03-15 15:44:18 +0000103Return any data available in the cooked queue (very lazy).
104
105Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +0000106Return \code{''} if no cooked data available otherwise. This method
107never blocks.
Fred Drake658cef01999-03-15 15:44:18 +0000108\end{methoddesc}
109
Guido van Rossumd8faa362007-04-27 19:54:29 +0000110\begin{methoddesc}[Telnet]{read_sb_data}{}
Martin v. Löwis1da9c572002-11-04 09:56:00 +0000111Return the data collected between a SB/SE pair (suboption begin/end).
112The callback should access these data when it was invoked with a
113\code{SE} command. This method never blocks.
114
115\versionadded{2.3}
116\end{methoddesc}
117
Guido van Rossumd8faa362007-04-27 19:54:29 +0000118\begin{methoddesc}[Telnet]{open}{host\optional{, port\optional{, timeout}}}
Fred Drake658cef01999-03-15 15:44:18 +0000119Connect to a host.
Fred Drake658cef01999-03-15 15:44:18 +0000120The optional second argument is the port number, which
Fred Drakeab1df4f2001-07-06 20:23:02 +0000121defaults to the standard Telnet port (23).
Guido van Rossumd8faa362007-04-27 19:54:29 +0000122The optional \var{timeout} parameter specifies a timeout in seconds for the
123connection attempt (if not specified, or passed as None, the global default
124timeout setting will be used).
Fred Drake658cef01999-03-15 15:44:18 +0000125
Fred Drakeb7168c31999-04-22 17:53:37 +0000126Do not try to reopen an already connected instance.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000127\versionchanged[\var{timeout} was added]{2.6}
Fred Drake658cef01999-03-15 15:44:18 +0000128\end{methoddesc}
129
Guido van Rossumd8faa362007-04-27 19:54:29 +0000130\begin{methoddesc}[Telnet]{msg}{msg\optional{, *args}}
Fred Drakeb7168c31999-04-22 17:53:37 +0000131Print a debug message when the debug level is \code{>} 0.
Fred Drake658cef01999-03-15 15:44:18 +0000132If extra arguments are present, they are substituted in the
133message using the standard string formatting operator.
134\end{methoddesc}
135
Guido van Rossumd8faa362007-04-27 19:54:29 +0000136\begin{methoddesc}[Telnet]{set_debuglevel}{debuglevel}
Fred Drakeb7168c31999-04-22 17:53:37 +0000137Set the debug level. The higher the value of \var{debuglevel}, the
138more debug output you get (on \code{sys.stdout}).
Fred Drake658cef01999-03-15 15:44:18 +0000139\end{methoddesc}
140
Guido van Rossumd8faa362007-04-27 19:54:29 +0000141\begin{methoddesc}[Telnet]{close}{}
Fred Drake658cef01999-03-15 15:44:18 +0000142Close the connection.
143\end{methoddesc}
144
Guido van Rossumd8faa362007-04-27 19:54:29 +0000145\begin{methoddesc}[Telnet]{get_socket}{}
Fred Drake658cef01999-03-15 15:44:18 +0000146Return the socket object used internally.
147\end{methoddesc}
148
Guido van Rossumd8faa362007-04-27 19:54:29 +0000149\begin{methoddesc}[Telnet]{fileno}{}
Fred Drakeb7168c31999-04-22 17:53:37 +0000150Return the file descriptor of the socket object used internally.
Fred Drake658cef01999-03-15 15:44:18 +0000151\end{methoddesc}
152
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153\begin{methoddesc}[Telnet]{write}{buffer}
Fred Drake658cef01999-03-15 15:44:18 +0000154Write a string to the socket, doubling any IAC characters.
Fred Drakeb7168c31999-04-22 17:53:37 +0000155This can block if the connection is blocked. May raise
156\exception{socket.error} if the connection is closed.
Fred Drake658cef01999-03-15 15:44:18 +0000157\end{methoddesc}
158
Guido van Rossumd8faa362007-04-27 19:54:29 +0000159\begin{methoddesc}[Telnet]{interact}{}
Fred Drakeab1df4f2001-07-06 20:23:02 +0000160Interaction function, emulates a very dumb Telnet client.
Fred Drake658cef01999-03-15 15:44:18 +0000161\end{methoddesc}
162
Guido van Rossumd8faa362007-04-27 19:54:29 +0000163\begin{methoddesc}[Telnet]{mt_interact}{}
Fred Drakeb7168c31999-04-22 17:53:37 +0000164Multithreaded version of \method{interact()}.
Fred Drake658cef01999-03-15 15:44:18 +0000165\end{methoddesc}
166
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167\begin{methoddesc}[Telnet]{expect}{list\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +0000168Read until one from a list of a regular expressions matches.
169
170The first argument is a list of regular expressions, either
171compiled (\class{re.RegexObject} instances) or uncompiled (strings).
Fred Drakeb7168c31999-04-22 17:53:37 +0000172The optional second argument is a timeout, in seconds; the default
Thomas Woutersf8316632000-07-16 19:01:10 +0000173is to block indefinitely.
Fred Drake658cef01999-03-15 15:44:18 +0000174
175Return a tuple of three items: the index in the list of the
176first regular expression that matches; the match object
177returned; and the text read up till and including the match.
178
179If end of file is found and no text was read, raise
180\exception{EOFError}. Otherwise, when nothing matches, return
181\code{(-1, None, \var{text})} where \var{text} is the text received so
182far (may be the empty string if a timeout happened).
183
Fred Drakeab1df4f2001-07-06 20:23:02 +0000184If a regular expression ends with a greedy match (such as \regexp{.*})
Fred Drake658cef01999-03-15 15:44:18 +0000185or if more than one expression can match the same input, the
Thomas Woutersf8316632000-07-16 19:01:10 +0000186results are indeterministic, and may depend on the I/O timing.
Fred Drake658cef01999-03-15 15:44:18 +0000187\end{methoddesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000188
Guido van Rossumd8faa362007-04-27 19:54:29 +0000189\begin{methoddesc}[Telnet]{set_option_negotiation_callback}{callback}
Martin v. Löwisb0162f92001-09-06 08:51:38 +0000190Each time a telnet option is read on the input flow, this
191\var{callback} (if set) is called with the following parameters :
192callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other
193action is done afterwards by telnetlib.
194\end{methoddesc}
195
Fred Drake38e5d272000-04-03 20:13:55 +0000196
197\subsection{Telnet Example \label{telnet-example}}
198\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
199
200A simple example illustrating typical use:
201
202\begin{verbatim}
203import getpass
204import sys
205import telnetlib
206
Neal Norwitzce96f692006-03-17 06:49:51 +0000207def raw_input(prompt):
208 sys.stdout.write(prompt)
209 sys.stdout.flush()
210 return sys.stdin.readline()
211
Fred Drake38e5d272000-04-03 20:13:55 +0000212HOST = "localhost"
213user = raw_input("Enter your remote account: ")
214password = getpass.getpass()
215
216tn = telnetlib.Telnet(HOST)
217
218tn.read_until("login: ")
219tn.write(user + "\n")
220if password:
221 tn.read_until("Password: ")
222 tn.write(password + "\n")
223
224tn.write("ls\n")
225tn.write("exit\n")
226
227print tn.read_all()
228\end{verbatim}