blob: de8895311de4978c7896b811d1b62f34b99641a3 [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
28\subsection{Telnet Objects \label{telnet-objects}}
29
30\class{Telnet} instances have the following methods:
31
32
Fred Drakeb7168c31999-04-22 17:53:37 +000033\begin{methoddesc}{read_until}{expected\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +000034Read until a given string is encountered or until timeout.
35
36When no match is found, return whatever is available instead,
37possibly the empty string. Raise \exception{EOFError} if the connection
38is closed and no cooked data is available.
39\end{methoddesc}
40
Fred Drakeb7168c31999-04-22 17:53:37 +000041\begin{methoddesc}{read_all}{}
42Read all data until \EOF{}; block until connection closed.
Fred Drake658cef01999-03-15 15:44:18 +000043\end{methoddesc}
44
Fred Drakeb7168c31999-04-22 17:53:37 +000045\begin{methoddesc}{read_some}{}
46Read at least one byte of cooked data unless \EOF{} is hit.
47Return \code{''} if \EOF{} is hit. Block if no data is immediately
48available.
Fred Drake658cef01999-03-15 15:44:18 +000049\end{methoddesc}
50
Fred Drakeb7168c31999-04-22 17:53:37 +000051\begin{methoddesc}{read_very_eager}{}
52Read everything that can be without blocking in I/O (eager).
Fred Drake658cef01999-03-15 15:44:18 +000053
54Raise \exception{EOFError} if connection closed and no cooked data
55available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000056Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000057\end{methoddesc}
58
Fred Drakeb7168c31999-04-22 17:53:37 +000059\begin{methoddesc}{read_eager}{}
Fred Drake658cef01999-03-15 15:44:18 +000060Read readily available data.
61
62Raise \exception{EOFError} if connection closed and no cooked data
63available. Return \code{''} if no cooked data available otherwise.
Fred Drakeb7168c31999-04-22 17:53:37 +000064Do not block unless in the midst of an IAC sequence.
Fred Drake658cef01999-03-15 15:44:18 +000065\end{methoddesc}
66
Fred Drakeb7168c31999-04-22 17:53:37 +000067\begin{methoddesc}{read_lazy}{}
68Process and return data already in the queues (lazy).
Fred Drake658cef01999-03-15 15:44:18 +000069
70Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000071Return \code{''} if no cooked data available otherwise. Do not block
Fred Drake658cef01999-03-15 15:44:18 +000072unless in the midst of an IAC sequence.
73\end{methoddesc}
74
Fred Drakeb7168c31999-04-22 17:53:37 +000075\begin{methoddesc}{read_very_lazy}{}
Fred Drake658cef01999-03-15 15:44:18 +000076Return any data available in the cooked queue (very lazy).
77
78Raise \exception{EOFError} if connection closed and no data available.
Fred Drakeb7168c31999-04-22 17:53:37 +000079Return \code{''} if no cooked data available otherwise. This method
80never blocks.
Fred Drake658cef01999-03-15 15:44:18 +000081\end{methoddesc}
82
Fred Drakeb7168c31999-04-22 17:53:37 +000083\begin{methoddesc}{open}{host\optional{, port}}
Fred Drake658cef01999-03-15 15:44:18 +000084Connect to a host.
Fred Drake658cef01999-03-15 15:44:18 +000085The optional second argument is the port number, which
86defaults to the standard telnet port (23).
87
Fred Drakeb7168c31999-04-22 17:53:37 +000088Do not try to reopen an already connected instance.
Fred Drake658cef01999-03-15 15:44:18 +000089\end{methoddesc}
90
Fred Drakeb7168c31999-04-22 17:53:37 +000091\begin{methoddesc}{msg}{msg\optional{, *args}}
92Print a debug message when the debug level is \code{>} 0.
Fred Drake658cef01999-03-15 15:44:18 +000093If extra arguments are present, they are substituted in the
94message using the standard string formatting operator.
95\end{methoddesc}
96
Fred Drakeb7168c31999-04-22 17:53:37 +000097\begin{methoddesc}{set_debuglevel}{debuglevel}
98Set the debug level. The higher the value of \var{debuglevel}, the
99more debug output you get (on \code{sys.stdout}).
Fred Drake658cef01999-03-15 15:44:18 +0000100\end{methoddesc}
101
Fred Drakeb7168c31999-04-22 17:53:37 +0000102\begin{methoddesc}{close}{}
Fred Drake658cef01999-03-15 15:44:18 +0000103Close the connection.
104\end{methoddesc}
105
Fred Drakeb7168c31999-04-22 17:53:37 +0000106\begin{methoddesc}{get_socket}{}
Fred Drake658cef01999-03-15 15:44:18 +0000107Return the socket object used internally.
108\end{methoddesc}
109
Fred Drakeb7168c31999-04-22 17:53:37 +0000110\begin{methoddesc}{fileno}{}
111Return the file descriptor of the socket object used internally.
Fred Drake658cef01999-03-15 15:44:18 +0000112\end{methoddesc}
113
Fred Drakeb7168c31999-04-22 17:53:37 +0000114\begin{methoddesc}{write}{buffer}
Fred Drake658cef01999-03-15 15:44:18 +0000115Write a string to the socket, doubling any IAC characters.
Fred Drakeb7168c31999-04-22 17:53:37 +0000116This can block if the connection is blocked. May raise
117\exception{socket.error} if the connection is closed.
Fred Drake658cef01999-03-15 15:44:18 +0000118\end{methoddesc}
119
Fred Drakeb7168c31999-04-22 17:53:37 +0000120\begin{methoddesc}{interact}{}
Fred Drake658cef01999-03-15 15:44:18 +0000121Interaction function, emulates a very dumb telnet client.
122\end{methoddesc}
123
Fred Drakeb7168c31999-04-22 17:53:37 +0000124\begin{methoddesc}{mt_interact}{}
125Multithreaded version of \method{interact()}.
Fred Drake658cef01999-03-15 15:44:18 +0000126\end{methoddesc}
127
Fred Drakeb7168c31999-04-22 17:53:37 +0000128\begin{methoddesc}{expect}{list\optional{, timeout}}
Fred Drake658cef01999-03-15 15:44:18 +0000129Read until one from a list of a regular expressions matches.
130
131The first argument is a list of regular expressions, either
132compiled (\class{re.RegexObject} instances) or uncompiled (strings).
Fred Drakeb7168c31999-04-22 17:53:37 +0000133The optional second argument is a timeout, in seconds; the default
134is to block indefinately.
Fred Drake658cef01999-03-15 15:44:18 +0000135
136Return a tuple of three items: the index in the list of the
137first regular expression that matches; the match object
138returned; and the text read up till and including the match.
139
140If end of file is found and no text was read, raise
141\exception{EOFError}. Otherwise, when nothing matches, return
142\code{(-1, None, \var{text})} where \var{text} is the text received so
143far (may be the empty string if a timeout happened).
144
145If a regular expression ends with a greedy match (e.g. \regexp{.*})
146or if more than one expression can match the same input, the
147results are undeterministic, and may depend on the I/O timing.
148\end{methoddesc}
Fred Drake38e5d272000-04-03 20:13:55 +0000149
150
151\subsection{Telnet Example \label{telnet-example}}
152\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
153
154A simple example illustrating typical use:
155
156\begin{verbatim}
157import getpass
158import sys
159import telnetlib
160
161HOST = "localhost"
162user = raw_input("Enter your remote account: ")
163password = getpass.getpass()
164
165tn = telnetlib.Telnet(HOST)
166
167tn.read_until("login: ")
168tn.write(user + "\n")
169if password:
170 tn.read_until("Password: ")
171 tn.write(password + "\n")
172
173tn.write("ls\n")
174tn.write("exit\n")
175
176print tn.read_all()
177\end{verbatim}