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