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