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