Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 1 | \section{\module{telnetlib} --- |
| 2 | Telnet client} |
| 3 | |
| 4 | \declaremodule{standard}{telnetlib} |
| 5 | \modulesynopsis{Telnet client class.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 6 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 7 | |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 8 | \index{protocol!Telnet} |
| 9 | |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 10 | The \module{telnetlib} module provides a \class{Telnet} class that |
| 11 | implements the Telnet protocol. See \rfc{854} for details about the |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 12 | protocol. In addition, it provides symbolic constants for the protocol |
Martin v. Löwis | 22610da | 2002-11-04 17:41:18 +0000 | [diff] [blame] | 13 | characters (see below), and for the telnet options. The |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 14 | symbolic names of the telnet options follow the definitions in |
| 15 | \code{arpa/telnet.h}, with the leading \code{TELOPT_} removed. For |
| 16 | symbolic names of options which are traditionally not included in |
| 17 | \code{arpa/telnet.h}, see the module source itself. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 18 | |
Martin v. Löwis | 22610da | 2002-11-04 17:41:18 +0000 | [diff] [blame] | 19 | The symbolic constants for the telnet commands are: IAC, DONT, DO, |
| 20 | WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data |
| 21 | Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT |
| 22 | (Are You There), EC (Erase Character), EL (Erase Line), GA (Go Ahead), |
| 23 | SB (Subnegotiation Begin). |
| 24 | |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 25 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 26 | \begin{classdesc}{Telnet}{\optional{host\optional{, port}}} |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 27 | \class{Telnet} represents a connection to a Telnet server. The |
Fred Drake | ae08853 | 2000-05-03 15:11:47 +0000 | [diff] [blame] | 28 | instance is initially not connected by default; the \method{open()} |
| 29 | method must be used to establish a connection. Alternatively, the |
| 30 | host name and optional port number can be passed to the constructor, |
| 31 | to, in which case the connection to the server will be established |
| 32 | before the constructor returns. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 33 | |
| 34 | Do not reopen an already connected instance. |
| 35 | |
| 36 | This class has many \method{read_*()} methods. Note that some of them |
| 37 | raise \exception{EOFError} when the end of the connection is read, |
| 38 | because they can return an empty string for other reasons. See the |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 39 | individual descriptions below. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 40 | \end{classdesc} |
| 41 | |
| 42 | |
Fred Drake | ac308d0 | 2000-04-26 18:20:04 +0000 | [diff] [blame] | 43 | \begin{seealso} |
| 44 | \seerfc{854}{Telnet Protocol Specification}{ |
| 45 | Definition of the Telnet protocol.} |
| 46 | \end{seealso} |
| 47 | |
| 48 | |
| 49 | |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 50 | \subsection{Telnet Objects \label{telnet-objects}} |
| 51 | |
| 52 | \class{Telnet} instances have the following methods: |
| 53 | |
| 54 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 55 | \begin{methoddesc}{read_until}{expected\optional{, timeout}} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 56 | Read until a given string is encountered or until timeout. |
| 57 | |
| 58 | When no match is found, return whatever is available instead, |
| 59 | possibly the empty string. Raise \exception{EOFError} if the connection |
| 60 | is closed and no cooked data is available. |
| 61 | \end{methoddesc} |
| 62 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 63 | \begin{methoddesc}{read_all}{} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 64 | Read all data until \EOF; block until connection closed. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 65 | \end{methoddesc} |
| 66 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 67 | \begin{methoddesc}{read_some}{} |
| 68 | Read at least one byte of cooked data unless \EOF{} is hit. |
| 69 | Return \code{''} if \EOF{} is hit. Block if no data is immediately |
| 70 | available. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 71 | \end{methoddesc} |
| 72 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 73 | \begin{methoddesc}{read_very_eager}{} |
| 74 | Read everything that can be without blocking in I/O (eager). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 75 | |
| 76 | Raise \exception{EOFError} if connection closed and no cooked data |
| 77 | available. Return \code{''} if no cooked data available otherwise. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 78 | Do not block unless in the midst of an IAC sequence. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 79 | \end{methoddesc} |
| 80 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 81 | \begin{methoddesc}{read_eager}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 82 | Read readily available data. |
| 83 | |
| 84 | Raise \exception{EOFError} if connection closed and no cooked data |
| 85 | available. Return \code{''} if no cooked data available otherwise. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 86 | Do not block unless in the midst of an IAC sequence. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 87 | \end{methoddesc} |
| 88 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 89 | \begin{methoddesc}{read_lazy}{} |
| 90 | Process and return data already in the queues (lazy). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 91 | |
| 92 | Raise \exception{EOFError} if connection closed and no data available. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 93 | Return \code{''} if no cooked data available otherwise. Do not block |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 94 | unless in the midst of an IAC sequence. |
| 95 | \end{methoddesc} |
| 96 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 97 | \begin{methoddesc}{read_very_lazy}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 98 | Return any data available in the cooked queue (very lazy). |
| 99 | |
| 100 | Raise \exception{EOFError} if connection closed and no data available. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 101 | Return \code{''} if no cooked data available otherwise. This method |
| 102 | never blocks. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 103 | \end{methoddesc} |
| 104 | |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 105 | \begin{methoddesc}{read_sb_data}{} |
| 106 | Return the data collected between a SB/SE pair (suboption begin/end). |
| 107 | The callback should access these data when it was invoked with a |
| 108 | \code{SE} command. This method never blocks. |
| 109 | |
| 110 | \versionadded{2.3} |
| 111 | \end{methoddesc} |
| 112 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 113 | \begin{methoddesc}{open}{host\optional{, port}} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 114 | Connect to a host. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 115 | The optional second argument is the port number, which |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 116 | defaults to the standard Telnet port (23). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 117 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 118 | Do not try to reopen an already connected instance. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 119 | \end{methoddesc} |
| 120 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 121 | \begin{methoddesc}{msg}{msg\optional{, *args}} |
| 122 | Print a debug message when the debug level is \code{>} 0. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 123 | If extra arguments are present, they are substituted in the |
| 124 | message using the standard string formatting operator. |
| 125 | \end{methoddesc} |
| 126 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 127 | \begin{methoddesc}{set_debuglevel}{debuglevel} |
| 128 | Set the debug level. The higher the value of \var{debuglevel}, the |
| 129 | more debug output you get (on \code{sys.stdout}). |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 130 | \end{methoddesc} |
| 131 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 132 | \begin{methoddesc}{close}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 133 | Close the connection. |
| 134 | \end{methoddesc} |
| 135 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 136 | \begin{methoddesc}{get_socket}{} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 137 | Return the socket object used internally. |
| 138 | \end{methoddesc} |
| 139 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 140 | \begin{methoddesc}{fileno}{} |
| 141 | Return the file descriptor of the socket object used internally. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 142 | \end{methoddesc} |
| 143 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 144 | \begin{methoddesc}{write}{buffer} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 145 | Write a string to the socket, doubling any IAC characters. |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 146 | This can block if the connection is blocked. May raise |
| 147 | \exception{socket.error} if the connection is closed. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 148 | \end{methoddesc} |
| 149 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 150 | \begin{methoddesc}{interact}{} |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 151 | Interaction function, emulates a very dumb Telnet client. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 152 | \end{methoddesc} |
| 153 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 154 | \begin{methoddesc}{mt_interact}{} |
| 155 | Multithreaded version of \method{interact()}. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 156 | \end{methoddesc} |
| 157 | |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 158 | \begin{methoddesc}{expect}{list\optional{, timeout}} |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 159 | Read until one from a list of a regular expressions matches. |
| 160 | |
| 161 | The first argument is a list of regular expressions, either |
| 162 | compiled (\class{re.RegexObject} instances) or uncompiled (strings). |
Fred Drake | b7168c3 | 1999-04-22 17:53:37 +0000 | [diff] [blame] | 163 | The optional second argument is a timeout, in seconds; the default |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 164 | is to block indefinitely. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 165 | |
| 166 | Return a tuple of three items: the index in the list of the |
| 167 | first regular expression that matches; the match object |
| 168 | returned; and the text read up till and including the match. |
| 169 | |
| 170 | If end of file is found and no text was read, raise |
| 171 | \exception{EOFError}. Otherwise, when nothing matches, return |
| 172 | \code{(-1, None, \var{text})} where \var{text} is the text received so |
| 173 | far (may be the empty string if a timeout happened). |
| 174 | |
Fred Drake | ab1df4f | 2001-07-06 20:23:02 +0000 | [diff] [blame] | 175 | If a regular expression ends with a greedy match (such as \regexp{.*}) |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 176 | or if more than one expression can match the same input, the |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 177 | results are indeterministic, and may depend on the I/O timing. |
Fred Drake | 658cef0 | 1999-03-15 15:44:18 +0000 | [diff] [blame] | 178 | \end{methoddesc} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 179 | |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 180 | \begin{methoddesc}{set_option_negotiation_callback}{callback} |
| 181 | Each time a telnet option is read on the input flow, this |
| 182 | \var{callback} (if set) is called with the following parameters : |
| 183 | callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other |
| 184 | action is done afterwards by telnetlib. |
| 185 | \end{methoddesc} |
| 186 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 187 | |
| 188 | \subsection{Telnet Example \label{telnet-example}} |
| 189 | \sectionauthor{Peter Funk}{pf@artcom-gmbh.de} |
| 190 | |
| 191 | A simple example illustrating typical use: |
| 192 | |
| 193 | \begin{verbatim} |
| 194 | import getpass |
| 195 | import sys |
| 196 | import telnetlib |
| 197 | |
| 198 | HOST = "localhost" |
| 199 | user = raw_input("Enter your remote account: ") |
| 200 | password = getpass.getpass() |
| 201 | |
| 202 | tn = telnetlib.Telnet(HOST) |
| 203 | |
| 204 | tn.read_until("login: ") |
| 205 | tn.write(user + "\n") |
| 206 | if password: |
| 207 | tn.read_until("Password: ") |
| 208 | tn.write(password + "\n") |
| 209 | |
| 210 | tn.write("ls\n") |
| 211 | tn.write("exit\n") |
| 212 | |
| 213 | print tn.read_all() |
| 214 | \end{verbatim} |