blob: fe56f72126c05a159a89f7576d1ea4c8e3e22bbf [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
13characters (IAC/DONT/DO/WONT/WILL), and for the telnet options. The
14symbolic 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
19
Fred Drakeb7168c31999-04-22 17:53:37 +000020\begin{classdesc}{Telnet}{\optional{host\optional{, port}}}
Fred Drakeab1df4f2001-07-06 20:23:02 +000021\class{Telnet} represents a connection to a Telnet server. The
Fred Drakeae088532000-05-03 15:11:47 +000022instance is initially not connected by default; the \method{open()}
23method must be used to establish a connection. Alternatively, the
24host name and optional port number can be passed to the constructor,
25to, in which case the connection to the server will be established
26before the constructor returns.
Fred Drake658cef01999-03-15 15:44:18 +000027
28Do not reopen an already connected instance.
29
30This class has many \method{read_*()} methods. Note that some of them
31raise \exception{EOFError} when the end of the connection is read,
32because they can return an empty string for other reasons. See the
Fred Drakeb7168c31999-04-22 17:53:37 +000033individual descriptions below.
Fred Drake658cef01999-03-15 15:44:18 +000034\end{classdesc}
35
36
Fred Drakeac308d02000-04-26 18:20:04 +000037\begin{seealso}
38 \seerfc{854}{Telnet Protocol Specification}{
39 Definition of the Telnet protocol.}
40\end{seealso}
41
42
43
Fred Drake658cef01999-03-15 15:44:18 +000044\subsection{Telnet Objects \label{telnet-objects}}
45
46\class{Telnet} instances have the following methods:
47
48
Fred Drakeb7168c31999-04-22 17:53:37 +000049\begin{methoddesc}{read_until}{expected\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +000050Read until a given string is encountered or until timeout.
51
52When no match is found, return whatever is available instead,
53possibly the empty string. Raise \exception{EOFError} if the connection
54is closed and no cooked data is available.
55\end{methoddesc}
56
Fred Drakeb7168c31999-04-22 17:53:37 +000057\begin{methoddesc}{read_all}{}
Fred Drakec37b65e2001-11-28 07:26:15 +000058Read all data until \EOF; block until connection closed.
Fred Drake658cef01999-03-15 15:44:18 +000059\end{methoddesc}
60
Fred Drakeb7168c31999-04-22 17:53:37 +000061\begin{methoddesc}{read_some}{}
62Read at least one byte of cooked data unless \EOF{} is hit.
63Return \code{''} if \EOF{} is hit. Block if no data is immediately
64available.
Fred Drake658cef01999-03-15 15:44:18 +000065\end{methoddesc}
66
Fred Drakeb7168c31999-04-22 17:53:37 +000067\begin{methoddesc}{read_very_eager}{}
68Read everything that can be without blocking in I/O (eager).
Fred Drake658cef01999-03-15 15:44:18 +000069
70Raise \exception{EOFError} if connection closed and no cooked data
71available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000072Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000073\end{methoddesc}
74
Fred Drakeb7168c31999-04-22 17:53:37 +000075\begin{methoddesc}{read_eager}{}
Fred Drake658cef01999-03-15 15:44:18 +000076Read readily available data.
77
78Raise \exception{EOFError} if connection closed and no cooked data
79available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000080Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000081\end{methoddesc}
82
Fred Drakeb7168c31999-04-22 17:53:37 +000083\begin{methoddesc}{read_lazy}{}
84Process and return data already in the queues (lazy).
Fred Drake658cef01999-03-15 15:44:18 +000085
86Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000087Return \code{''} if no cooked data available otherwise. Do not block
Fred Drake658cef01999-03-15 15:44:18 +000088unless in the midst of an IAC sequence.
89\end{methoddesc}
90
Fred Drakeb7168c31999-04-22 17:53:37 +000091\begin{methoddesc}{read_very_lazy}{}
Fred Drake658cef01999-03-15 15:44:18 +000092Return any data available in the cooked queue (very lazy).
93
94Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000095Return \code{''} if no cooked data available otherwise. This method
96never blocks.
Fred Drake658cef01999-03-15 15:44:18 +000097\end{methoddesc}
98
Fred Drakeb7168c31999-04-22 17:53:37 +000099\begin{methoddesc}{open}{host\optional{, port}}
Fred Drake658cef01999-03-15 15:44:18 +0000100Connect to a host.
Fred Drake658cef01999-03-15 15:44:18 +0000101The optional second argument is the port number, which
Fred Drakeab1df4f2001-07-06 20:23:02 +0000102defaults to the standard Telnet port (23).
Fred Drake658cef01999-03-15 15:44:18 +0000103
Fred Drakeb7168c31999-04-22 17:53:37 +0000104Do not try to reopen an already connected instance.
Fred Drake658cef01999-03-15 15:44:18 +0000105\end{methoddesc}
106
Fred Drakeb7168c31999-04-22 17:53:37 +0000107\begin{methoddesc}{msg}{msg\optional{, *args}}
108Print a debug message when the debug level is \code{>} 0.
Fred Drake658cef01999-03-15 15:44:18 +0000109If extra arguments are present, they are substituted in the
110message using the standard string formatting operator.
111\end{methoddesc}
112
Fred Drakeb7168c31999-04-22 17:53:37 +0000113\begin{methoddesc}{set_debuglevel}{debuglevel}
114Set the debug level. The higher the value of \var{debuglevel}, the
115more debug output you get (on \code{sys.stdout}).
Fred Drake658cef01999-03-15 15:44:18 +0000116\end{methoddesc}
117
Fred Drakeb7168c31999-04-22 17:53:37 +0000118\begin{methoddesc}{close}{}
Fred Drake658cef01999-03-15 15:44:18 +0000119Close the connection.
120\end{methoddesc}
121
Fred Drakeb7168c31999-04-22 17:53:37 +0000122\begin{methoddesc}{get_socket}{}
Fred Drake658cef01999-03-15 15:44:18 +0000123Return the socket object used internally.
124\end{methoddesc}
125
Fred Drakeb7168c31999-04-22 17:53:37 +0000126\begin{methoddesc}{fileno}{}
127Return the file descriptor of the socket object used internally.
Fred Drake658cef01999-03-15 15:44:18 +0000128\end{methoddesc}
129
Fred Drakeb7168c31999-04-22 17:53:37 +0000130\begin{methoddesc}{write}{buffer}
Fred Drake658cef01999-03-15 15:44:18 +0000131Write a string to the socket, doubling any IAC characters.
Fred Drakeb7168c31999-04-22 17:53:37 +0000132This can block if the connection is blocked. May raise
133\exception{socket.error} if the connection is closed.
Fred Drake658cef01999-03-15 15:44:18 +0000134\end{methoddesc}
135
Fred Drakeb7168c31999-04-22 17:53:37 +0000136\begin{methoddesc}{interact}{}
Fred Drakeab1df4f2001-07-06 20:23:02 +0000137Interaction function, emulates a very dumb Telnet client.
Fred Drake658cef01999-03-15 15:44:18 +0000138\end{methoddesc}
139
Fred Drakeb7168c31999-04-22 17:53:37 +0000140\begin{methoddesc}{mt_interact}{}
141Multithreaded version of \method{interact()}.
Fred Drake658cef01999-03-15 15:44:18 +0000142\end{methoddesc}
143
Fred Drakeb7168c31999-04-22 17:53:37 +0000144\begin{methoddesc}{expect}{list\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +0000145Read until one from a list of a regular expressions matches.
146
147The first argument is a list of regular expressions, either
148compiled (\class{re.RegexObject} instances) or uncompiled (strings).
Fred Drakeb7168c31999-04-22 17:53:37 +0000149The optional second argument is a timeout, in seconds; the default
Thomas Woutersf8316632000-07-16 19:01:10 +0000150is to block indefinitely.
Fred Drake658cef01999-03-15 15:44:18 +0000151
152Return a tuple of three items: the index in the list of the
153first regular expression that matches; the match object
154returned; and the text read up till and including the match.
155
156If end of file is found and no text was read, raise
157\exception{EOFError}. Otherwise, when nothing matches, return
158\code{(-1, None, \var{text})} where \var{text} is the text received so
159far (may be the empty string if a timeout happened).
160
Fred Drakeab1df4f2001-07-06 20:23:02 +0000161If a regular expression ends with a greedy match (such as \regexp{.*})
Fred Drake658cef01999-03-15 15:44:18 +0000162or if more than one expression can match the same input, the
Thomas Woutersf8316632000-07-16 19:01:10 +0000163results are indeterministic, and may depend on the I/O timing.
Fred Drake658cef01999-03-15 15:44:18 +0000164\end{methoddesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000165
Martin v. Löwisb0162f92001-09-06 08:51:38 +0000166\begin{methoddesc}{set_option_negotiation_callback}{callback}
167Each time a telnet option is read on the input flow, this
168\var{callback} (if set) is called with the following parameters :
169callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other
170action is done afterwards by telnetlib.
171\end{methoddesc}
172
Fred Drake38e5d272000-04-03 20:13:55 +0000173
174\subsection{Telnet Example \label{telnet-example}}
175\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
176
177A simple example illustrating typical use:
178
179\begin{verbatim}
180import getpass
181import sys
182import telnetlib
183
184HOST = "localhost"
185user = raw_input("Enter your remote account: ")
186password = getpass.getpass()
187
188tn = telnetlib.Telnet(HOST)
189
190tn.read_until("login: ")
191tn.write(user + "\n")
192if password:
193 tn.read_until("Password: ")
194 tn.write(password + "\n")
195
196tn.write("ls\n")
197tn.write("exit\n")
198
199print tn.read_all()
200\end{verbatim}