blob: c772839d2bf563e93565452418d6bbee1b97927b [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
12protocol.
13
14
Fred Drakeb7168c31999-04-22 17:53:37 +000015\begin{classdesc}{Telnet}{\optional{host\optional{, port}}}
Fred Drakeab1df4f2001-07-06 20:23:02 +000016\class{Telnet} represents a connection to a Telnet server. The
Fred Drakeae088532000-05-03 15:11:47 +000017instance is initially not connected by default; the \method{open()}
18method must be used to establish a connection. Alternatively, the
19host name and optional port number can be passed to the constructor,
20to, in which case the connection to the server will be established
21before the constructor returns.
Fred Drake658cef01999-03-15 15:44:18 +000022
23Do not reopen an already connected instance.
24
25This class has many \method{read_*()} methods. Note that some of them
26raise \exception{EOFError} when the end of the connection is read,
27because they can return an empty string for other reasons. See the
Fred Drakeb7168c31999-04-22 17:53:37 +000028individual descriptions below.
Fred Drake658cef01999-03-15 15:44:18 +000029\end{classdesc}
30
31
Fred Drakeac308d02000-04-26 18:20:04 +000032\begin{seealso}
33 \seerfc{854}{Telnet Protocol Specification}{
34 Definition of the Telnet protocol.}
35\end{seealso}
36
37
38
Fred Drake658cef01999-03-15 15:44:18 +000039\subsection{Telnet Objects \label{telnet-objects}}
40
41\class{Telnet} instances have the following methods:
42
43
Fred Drakeb7168c31999-04-22 17:53:37 +000044\begin{methoddesc}{read_until}{expected\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +000045Read until a given string is encountered or until timeout.
46
47When no match is found, return whatever is available instead,
48possibly the empty string. Raise \exception{EOFError} if the connection
49is closed and no cooked data is available.
50\end{methoddesc}
51
Fred Drakeb7168c31999-04-22 17:53:37 +000052\begin{methoddesc}{read_all}{}
53Read all data until \EOF{}; block until connection closed.
Fred Drake658cef01999-03-15 15:44:18 +000054\end{methoddesc}
55
Fred Drakeb7168c31999-04-22 17:53:37 +000056\begin{methoddesc}{read_some}{}
57Read at least one byte of cooked data unless \EOF{} is hit.
58Return \code{''} if \EOF{} is hit. Block if no data is immediately
59available.
Fred Drake658cef01999-03-15 15:44:18 +000060\end{methoddesc}
61
Fred Drakeb7168c31999-04-22 17:53:37 +000062\begin{methoddesc}{read_very_eager}{}
63Read everything that can be without blocking in I/O (eager).
Fred Drake658cef01999-03-15 15:44:18 +000064
65Raise \exception{EOFError} if connection closed and no cooked data
66available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000067Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000068\end{methoddesc}
69
Fred Drakeb7168c31999-04-22 17:53:37 +000070\begin{methoddesc}{read_eager}{}
Fred Drake658cef01999-03-15 15:44:18 +000071Read readily available data.
72
73Raise \exception{EOFError} if connection closed and no cooked data
74available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000075Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000076\end{methoddesc}
77
Fred Drakeb7168c31999-04-22 17:53:37 +000078\begin{methoddesc}{read_lazy}{}
79Process and return data already in the queues (lazy).
Fred Drake658cef01999-03-15 15:44:18 +000080
81Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000082Return \code{''} if no cooked data available otherwise. Do not block
Fred Drake658cef01999-03-15 15:44:18 +000083unless in the midst of an IAC sequence.
84\end{methoddesc}
85
Fred Drakeb7168c31999-04-22 17:53:37 +000086\begin{methoddesc}{read_very_lazy}{}
Fred Drake658cef01999-03-15 15:44:18 +000087Return any data available in the cooked queue (very lazy).
88
89Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000090Return \code{''} if no cooked data available otherwise. This method
91never blocks.
Fred Drake658cef01999-03-15 15:44:18 +000092\end{methoddesc}
93
Fred Drakeb7168c31999-04-22 17:53:37 +000094\begin{methoddesc}{open}{host\optional{, port}}
Fred Drake658cef01999-03-15 15:44:18 +000095Connect to a host.
Fred Drake658cef01999-03-15 15:44:18 +000096The optional second argument is the port number, which
Fred Drakeab1df4f2001-07-06 20:23:02 +000097defaults to the standard Telnet port (23).
Fred Drake658cef01999-03-15 15:44:18 +000098
Fred Drakeb7168c31999-04-22 17:53:37 +000099Do not try to reopen an already connected instance.
Fred Drake658cef01999-03-15 15:44:18 +0000100\end{methoddesc}
101
Fred Drakeb7168c31999-04-22 17:53:37 +0000102\begin{methoddesc}{msg}{msg\optional{, *args}}
103Print a debug message when the debug level is \code{>} 0.
Fred Drake658cef01999-03-15 15:44:18 +0000104If extra arguments are present, they are substituted in the
105message using the standard string formatting operator.
106\end{methoddesc}
107
Fred Drakeb7168c31999-04-22 17:53:37 +0000108\begin{methoddesc}{set_debuglevel}{debuglevel}
109Set the debug level. The higher the value of \var{debuglevel}, the
110more debug output you get (on \code{sys.stdout}).
Fred Drake658cef01999-03-15 15:44:18 +0000111\end{methoddesc}
112
Fred Drakeb7168c31999-04-22 17:53:37 +0000113\begin{methoddesc}{close}{}
Fred Drake658cef01999-03-15 15:44:18 +0000114Close the connection.
115\end{methoddesc}
116
Fred Drakeb7168c31999-04-22 17:53:37 +0000117\begin{methoddesc}{get_socket}{}
Fred Drake658cef01999-03-15 15:44:18 +0000118Return the socket object used internally.
119\end{methoddesc}
120
Fred Drakeb7168c31999-04-22 17:53:37 +0000121\begin{methoddesc}{fileno}{}
122Return the file descriptor of the socket object used internally.
Fred Drake658cef01999-03-15 15:44:18 +0000123\end{methoddesc}
124
Fred Drakeb7168c31999-04-22 17:53:37 +0000125\begin{methoddesc}{write}{buffer}
Fred Drake658cef01999-03-15 15:44:18 +0000126Write a string to the socket, doubling any IAC characters.
Fred Drakeb7168c31999-04-22 17:53:37 +0000127This can block if the connection is blocked. May raise
128\exception{socket.error} if the connection is closed.
Fred Drake658cef01999-03-15 15:44:18 +0000129\end{methoddesc}
130
Fred Drakeb7168c31999-04-22 17:53:37 +0000131\begin{methoddesc}{interact}{}
Fred Drakeab1df4f2001-07-06 20:23:02 +0000132Interaction function, emulates a very dumb Telnet client.
Fred Drake658cef01999-03-15 15:44:18 +0000133\end{methoddesc}
134
Fred Drakeb7168c31999-04-22 17:53:37 +0000135\begin{methoddesc}{mt_interact}{}
136Multithreaded version of \method{interact()}.
Fred Drake658cef01999-03-15 15:44:18 +0000137\end{methoddesc}
138
Fred Drakeb7168c31999-04-22 17:53:37 +0000139\begin{methoddesc}{expect}{list\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +0000140Read until one from a list of a regular expressions matches.
141
142The first argument is a list of regular expressions, either
143compiled (\class{re.RegexObject} instances) or uncompiled (strings).
Fred Drakeb7168c31999-04-22 17:53:37 +0000144The optional second argument is a timeout, in seconds; the default
Thomas Woutersf8316632000-07-16 19:01:10 +0000145is to block indefinitely.
Fred Drake658cef01999-03-15 15:44:18 +0000146
147Return a tuple of three items: the index in the list of the
148first regular expression that matches; the match object
149returned; and the text read up till and including the match.
150
151If end of file is found and no text was read, raise
152\exception{EOFError}. Otherwise, when nothing matches, return
153\code{(-1, None, \var{text})} where \var{text} is the text received so
154far (may be the empty string if a timeout happened).
155
Fred Drakeab1df4f2001-07-06 20:23:02 +0000156If a regular expression ends with a greedy match (such as \regexp{.*})
Fred Drake658cef01999-03-15 15:44:18 +0000157or if more than one expression can match the same input, the
Thomas Woutersf8316632000-07-16 19:01:10 +0000158results are indeterministic, and may depend on the I/O timing.
Fred Drake658cef01999-03-15 15:44:18 +0000159\end{methoddesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000160
161
162\subsection{Telnet Example \label{telnet-example}}
163\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
164
165A simple example illustrating typical use:
166
167\begin{verbatim}
168import getpass
169import sys
170import telnetlib
171
172HOST = "localhost"
173user = raw_input("Enter your remote account: ")
174password = getpass.getpass()
175
176tn = telnetlib.Telnet(HOST)
177
178tn.read_until("login: ")
179tn.write(user + "\n")
180if password:
181 tn.read_until("Password: ")
182 tn.write(password + "\n")
183
184tn.write("ls\n")
185tn.write("exit\n")
186
187print tn.read_all()
188\end{verbatim}