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