Hye-Shik Chang | e029da0 | 2005-09-07 07:40:05 +0000 | [diff] [blame] | 1 | r"""TELNET client class. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 2 | |
| 3 | Based on RFC 854: TELNET Protocol Specification, by J. Postel and |
| 4 | J. Reynolds |
| 5 | |
| 6 | Example: |
| 7 | |
| 8 | >>> from telnetlib import Telnet |
| 9 | >>> tn = Telnet('www.python.org', 79) # connect to finger port |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 10 | >>> tn.write(b'guido\r\n') |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 11 | >>> print(tn.read_all()) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 12 | Login Name TTY Idle When Where |
| 13 | guido Guido van Rossum pts/2 <Dec 2 11:10> snag.cnri.reston.. |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 15 | >>> |
| 16 | |
| 17 | Note that read_all() won't read until eof -- it just reads some data |
| 18 | -- but it guarantees to read at least one byte unless EOF is hit. |
| 19 | |
| 20 | It is possible to pass a Telnet object to select.select() in order to |
| 21 | wait until more data is available. Note that in this case, |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 22 | read_eager() may return b'' even if there was data on the socket, |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 23 | because the protocol negotiation may have eaten the data. This is why |
| 24 | EOFError is needed in some cases to distinguish between "no data" and |
| 25 | "connection closed" (since the socket also appears ready for reading |
| 26 | when it is closed). |
| 27 | |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 28 | To do: |
| 29 | - option negotiation |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 30 | - timeout should be intrinsic to the connection object instead of an |
| 31 | option on one of the read calls only |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 32 | |
| 33 | """ |
| 34 | |
| 35 | |
| 36 | # Imported modules |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 37 | import sys |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 38 | import socket |
| 39 | import select |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 40 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 41 | __all__ = ["Telnet"] |
| 42 | |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 43 | # Tunable parameters |
| 44 | DEBUGLEVEL = 0 |
| 45 | |
| 46 | # Telnet protocol defaults |
| 47 | TELNET_PORT = 23 |
| 48 | |
| 49 | # Telnet protocol characters (don't change) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 50 | IAC = bytes([255]) # "Interpret As Command" |
| 51 | DONT = bytes([254]) |
| 52 | DO = bytes([253]) |
| 53 | WONT = bytes([252]) |
| 54 | WILL = bytes([251]) |
| 55 | theNULL = bytes([0]) |
Martin v. Löwis | 574deae | 2002-11-04 17:34:07 +0000 | [diff] [blame] | 56 | |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 57 | SE = bytes([240]) # Subnegotiation End |
| 58 | NOP = bytes([241]) # No Operation |
| 59 | DM = bytes([242]) # Data Mark |
| 60 | BRK = bytes([243]) # Break |
| 61 | IP = bytes([244]) # Interrupt process |
| 62 | AO = bytes([245]) # Abort output |
| 63 | AYT = bytes([246]) # Are You There |
| 64 | EC = bytes([247]) # Erase Character |
| 65 | EL = bytes([248]) # Erase Line |
| 66 | GA = bytes([249]) # Go Ahead |
| 67 | SB = bytes([250]) # Subnegotiation Begin |
Martin v. Löwis | 574deae | 2002-11-04 17:34:07 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 69 | |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 70 | # Telnet protocol options code (don't change) |
| 71 | # These ones all come from arpa/telnet.h |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 72 | BINARY = bytes([0]) # 8-bit data path |
| 73 | ECHO = bytes([1]) # echo |
| 74 | RCP = bytes([2]) # prepare to reconnect |
| 75 | SGA = bytes([3]) # suppress go ahead |
| 76 | NAMS = bytes([4]) # approximate message size |
| 77 | STATUS = bytes([5]) # give status |
| 78 | TM = bytes([6]) # timing mark |
| 79 | RCTE = bytes([7]) # remote controlled transmission and echo |
| 80 | NAOL = bytes([8]) # negotiate about output line width |
| 81 | NAOP = bytes([9]) # negotiate about output page size |
| 82 | NAOCRD = bytes([10]) # negotiate about CR disposition |
| 83 | NAOHTS = bytes([11]) # negotiate about horizontal tabstops |
| 84 | NAOHTD = bytes([12]) # negotiate about horizontal tab disposition |
| 85 | NAOFFD = bytes([13]) # negotiate about formfeed disposition |
| 86 | NAOVTS = bytes([14]) # negotiate about vertical tab stops |
| 87 | NAOVTD = bytes([15]) # negotiate about vertical tab disposition |
| 88 | NAOLFD = bytes([16]) # negotiate about output LF disposition |
| 89 | XASCII = bytes([17]) # extended ascii character set |
| 90 | LOGOUT = bytes([18]) # force logout |
| 91 | BM = bytes([19]) # byte macro |
| 92 | DET = bytes([20]) # data entry terminal |
| 93 | SUPDUP = bytes([21]) # supdup protocol |
| 94 | SUPDUPOUTPUT = bytes([22]) # supdup output |
| 95 | SNDLOC = bytes([23]) # send location |
| 96 | TTYPE = bytes([24]) # terminal type |
| 97 | EOR = bytes([25]) # end or record |
| 98 | TUID = bytes([26]) # TACACS user identification |
| 99 | OUTMRK = bytes([27]) # output marking |
| 100 | TTYLOC = bytes([28]) # terminal location number |
| 101 | VT3270REGIME = bytes([29]) # 3270 regime |
| 102 | X3PAD = bytes([30]) # X.3 PAD |
| 103 | NAWS = bytes([31]) # window size |
| 104 | TSPEED = bytes([32]) # terminal speed |
| 105 | LFLOW = bytes([33]) # remote flow control |
| 106 | LINEMODE = bytes([34]) # Linemode option |
| 107 | XDISPLOC = bytes([35]) # X Display Location |
| 108 | OLD_ENVIRON = bytes([36]) # Old - Environment variables |
| 109 | AUTHENTICATION = bytes([37]) # Authenticate |
| 110 | ENCRYPT = bytes([38]) # Encryption option |
| 111 | NEW_ENVIRON = bytes([39]) # New - Environment variables |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 112 | # the following ones come from |
| 113 | # http://www.iana.org/assignments/telnet-options |
| 114 | # Unfortunately, that document does not assign identifiers |
| 115 | # to all of them, so we are making them up |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 116 | TN3270E = bytes([40]) # TN3270E |
| 117 | XAUTH = bytes([41]) # XAUTH |
| 118 | CHARSET = bytes([42]) # CHARSET |
| 119 | RSP = bytes([43]) # Telnet Remote Serial Port |
| 120 | COM_PORT_OPTION = bytes([44]) # Com Port Control Option |
| 121 | SUPPRESS_LOCAL_ECHO = bytes([45]) # Telnet Suppress Local Echo |
| 122 | TLS = bytes([46]) # Telnet Start TLS |
| 123 | KERMIT = bytes([47]) # KERMIT |
| 124 | SEND_URL = bytes([48]) # SEND-URL |
| 125 | FORWARD_X = bytes([49]) # FORWARD_X |
| 126 | PRAGMA_LOGON = bytes([138]) # TELOPT PRAGMA LOGON |
| 127 | SSPI_LOGON = bytes([139]) # TELOPT SSPI LOGON |
| 128 | PRAGMA_HEARTBEAT = bytes([140]) # TELOPT PRAGMA HEARTBEAT |
| 129 | EXOPL = bytes([255]) # Extended-Options-List |
| 130 | NOOPT = bytes([0]) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 131 | |
| 132 | class Telnet: |
| 133 | |
| 134 | """Telnet interface class. |
| 135 | |
| 136 | An instance of this class represents a connection to a telnet |
| 137 | server. The instance is initially not connected; the open() |
| 138 | method must be used to establish a connection. Alternatively, the |
| 139 | host name and optional port number can be passed to the |
| 140 | constructor, too. |
| 141 | |
| 142 | Don't try to reopen an already connected instance. |
| 143 | |
| 144 | This class has many read_*() methods. Note that some of them |
| 145 | raise EOFError when the end of the connection is read, because |
| 146 | they can return an empty string for other reasons. See the |
| 147 | individual doc strings. |
| 148 | |
| 149 | read_until(expected, [timeout]) |
| 150 | Read until the expected string has been seen, or a timeout is |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | hit (default is no timeout); may block. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 152 | |
| 153 | read_all() |
| 154 | Read all data until EOF; may block. |
| 155 | |
| 156 | read_some() |
| 157 | Read at least one byte or EOF; may block. |
| 158 | |
| 159 | read_very_eager() |
| 160 | Read all data available already queued or on the socket, |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 161 | without blocking. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 162 | |
| 163 | read_eager() |
| 164 | Read either data already queued or some data available on the |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | socket, without blocking. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 166 | |
| 167 | read_lazy() |
| 168 | Read all data in the raw queue (processing it first), without |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | doing any socket I/O. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 170 | |
| 171 | read_very_lazy() |
| 172 | Reads all data in the cooked queue, without doing any socket |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 173 | I/O. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 174 | |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 175 | read_sb_data() |
| 176 | Reads available data between SB ... SE sequence. Don't block. |
| 177 | |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 178 | set_option_negotiation_callback(callback) |
| 179 | Each time a telnet option is read on the input flow, this callback |
| 180 | (if set) is called with the following parameters : |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 181 | callback(telnet socket, command, option) |
| 182 | option will be chr(0) when there is no option. |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 183 | No other action is done afterwards by telnetlib. |
| 184 | |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 185 | """ |
| 186 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 187 | def __init__(self, host=None, port=0, |
| 188 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | """Constructor. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 191 | When called without arguments, create an unconnected instance. |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 192 | With a hostname argument, it connects the instance; port number |
| 193 | and timeout are optional. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 194 | """ |
| 195 | self.debuglevel = DEBUGLEVEL |
| 196 | self.host = host |
| 197 | self.port = port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 198 | self.timeout = timeout |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 199 | self.sock = None |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 200 | self.rawq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | self.irawq = 0 |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 202 | self.cookedq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 203 | self.eof = 0 |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 204 | self.iacseq = b'' # Buffer for IAC sequence. |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 205 | self.sb = 0 # flag for SB and SE sequence. |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 206 | self.sbdataq = b'' |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 207 | self.option_callback = None |
Raymond Hettinger | 10ff706 | 2002-06-02 03:04:52 +0000 | [diff] [blame] | 208 | if host is not None: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 209 | self.open(host, port, timeout) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 210 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 211 | def open(self, host, port=0, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 212 | """Connect to a host. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 214 | The optional second argument is the port number, which |
| 215 | defaults to the standard telnet port (23). |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 217 | Don't try to reopen an already connected instance. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 218 | """ |
| 219 | self.eof = 0 |
| 220 | if not port: |
| 221 | port = TELNET_PORT |
| 222 | self.host = host |
| 223 | self.port = port |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 224 | self.timeout = timeout |
| 225 | self.sock = socket.create_connection((host, port), timeout) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 226 | |
| 227 | def __del__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 228 | """Destructor -- close the connection.""" |
| 229 | self.close() |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 230 | |
| 231 | def msg(self, msg, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 232 | """Print a debug message, when the debug level is > 0. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 234 | If extra arguments are present, they are substituted in the |
| 235 | message using the standard string formatting operator. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 237 | """ |
| 238 | if self.debuglevel > 0: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 239 | print('Telnet(%s,%d):' % (self.host, self.port), end=' ') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 240 | if args: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 241 | print(msg % args) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 242 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 243 | print(msg) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 244 | |
| 245 | def set_debuglevel(self, debuglevel): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 246 | """Set the debug level. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 248 | The higher it is, the more debug output you get (on sys.stdout). |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 250 | """ |
| 251 | self.debuglevel = debuglevel |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 252 | |
| 253 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 254 | """Close the connection.""" |
| 255 | if self.sock: |
| 256 | self.sock.close() |
| 257 | self.sock = 0 |
| 258 | self.eof = 1 |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 259 | self.iacseq = b'' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 260 | self.sb = 0 |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 261 | |
| 262 | def get_socket(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 263 | """Return the socket object used internally.""" |
| 264 | return self.sock |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 265 | |
| 266 | def fileno(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 267 | """Return the fileno() of the socket object used internally.""" |
| 268 | return self.sock.fileno() |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 269 | |
| 270 | def write(self, buffer): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 271 | """Write a string to the socket, doubling any IAC characters. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 273 | Can block if the connection is blocked. May raise |
| 274 | socket.error if the connection is closed. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 276 | """ |
| 277 | if IAC in buffer: |
Eric S. Raymond | 6b8c528 | 2001-02-09 07:10:12 +0000 | [diff] [blame] | 278 | buffer = buffer.replace(IAC, IAC+IAC) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 279 | self.msg("send %r", buffer) |
Martin v. Löwis | e12454f | 2002-02-16 23:06:19 +0000 | [diff] [blame] | 280 | self.sock.sendall(buffer) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 281 | |
| 282 | def read_until(self, match, timeout=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 283 | """Read until a given string is encountered or until timeout. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 285 | When no match is found, return whatever is available instead, |
| 286 | possibly the empty string. Raise EOFError if the connection |
| 287 | is closed and no cooked data is available. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 289 | """ |
| 290 | n = len(match) |
| 291 | self.process_rawq() |
Eric S. Raymond | 6b8c528 | 2001-02-09 07:10:12 +0000 | [diff] [blame] | 292 | i = self.cookedq.find(match) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 293 | if i >= 0: |
| 294 | i = i+n |
| 295 | buf = self.cookedq[:i] |
| 296 | self.cookedq = self.cookedq[i:] |
| 297 | return buf |
| 298 | s_reply = ([self], [], []) |
| 299 | s_args = s_reply |
| 300 | if timeout is not None: |
| 301 | s_args = s_args + (timeout,) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 302 | from time import time |
| 303 | time_start = time() |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 304 | while not self.eof and select.select(*s_args) == s_reply: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 305 | i = max(0, len(self.cookedq)-n) |
| 306 | self.fill_rawq() |
| 307 | self.process_rawq() |
Eric S. Raymond | 6b8c528 | 2001-02-09 07:10:12 +0000 | [diff] [blame] | 308 | i = self.cookedq.find(match, i) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 309 | if i >= 0: |
| 310 | i = i+n |
| 311 | buf = self.cookedq[:i] |
| 312 | self.cookedq = self.cookedq[i:] |
| 313 | return buf |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 314 | if timeout is not None: |
| 315 | elapsed = time() - time_start |
| 316 | if elapsed >= timeout: |
| 317 | break |
| 318 | s_args = s_reply + (timeout-elapsed,) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 319 | return self.read_very_lazy() |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 320 | |
| 321 | def read_all(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 322 | """Read all data until EOF; block until connection closed.""" |
| 323 | self.process_rawq() |
| 324 | while not self.eof: |
| 325 | self.fill_rawq() |
| 326 | self.process_rawq() |
| 327 | buf = self.cookedq |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 328 | self.cookedq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 329 | return buf |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 330 | |
| 331 | def read_some(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 332 | """Read at least one byte of cooked data unless EOF is hit. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 333 | |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 334 | Return b'' if EOF is hit. Block if no data is immediately |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 335 | available. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 337 | """ |
| 338 | self.process_rawq() |
| 339 | while not self.cookedq and not self.eof: |
| 340 | self.fill_rawq() |
| 341 | self.process_rawq() |
| 342 | buf = self.cookedq |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 343 | self.cookedq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 344 | return buf |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 345 | |
| 346 | def read_very_eager(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 347 | """Read everything that's possible without blocking in I/O (eager). |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 349 | Raise EOFError if connection closed and no cooked data |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 350 | available. Return b'' if no cooked data available otherwise. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 351 | Don't block unless in the midst of an IAC sequence. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 353 | """ |
| 354 | self.process_rawq() |
| 355 | while not self.eof and self.sock_avail(): |
| 356 | self.fill_rawq() |
| 357 | self.process_rawq() |
| 358 | return self.read_very_lazy() |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 359 | |
| 360 | def read_eager(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 361 | """Read readily available data. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 362 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 363 | Raise EOFError if connection closed and no cooked data |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 364 | available. Return b'' if no cooked data available otherwise. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 365 | Don't block unless in the midst of an IAC sequence. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 366 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 367 | """ |
| 368 | self.process_rawq() |
| 369 | while not self.cookedq and not self.eof and self.sock_avail(): |
| 370 | self.fill_rawq() |
| 371 | self.process_rawq() |
| 372 | return self.read_very_lazy() |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 373 | |
| 374 | def read_lazy(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 375 | """Process and return data that's already in the queues (lazy). |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 377 | Raise EOFError if connection closed and no data available. |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 378 | Return b'' if no cooked data available otherwise. Don't block |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 379 | unless in the midst of an IAC sequence. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 381 | """ |
| 382 | self.process_rawq() |
| 383 | return self.read_very_lazy() |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 384 | |
| 385 | def read_very_lazy(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 386 | """Return any data available in the cooked queue (very lazy). |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 387 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 388 | Raise EOFError if connection closed and no data available. |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 389 | Return b'' if no cooked data available otherwise. Don't block. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 391 | """ |
| 392 | buf = self.cookedq |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 393 | self.cookedq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 394 | if not buf and self.eof and not self.rawq: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 395 | raise EOFError('telnet connection closed') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 396 | return buf |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 397 | |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 398 | def read_sb_data(self): |
| 399 | """Return any data available in the SB ... SE queue. |
| 400 | |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 401 | Return b'' if no SB ... SE available. Should only be called |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 402 | after seeing a SB or SE command. When a new SB command is |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 403 | found, old unread SB data will be discarded. Don't block. |
| 404 | |
| 405 | """ |
| 406 | buf = self.sbdataq |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 407 | self.sbdataq = b'' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 408 | return buf |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 409 | |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 410 | def set_option_negotiation_callback(self, callback): |
| 411 | """Provide a callback function called after each receipt of a telnet option.""" |
| 412 | self.option_callback = callback |
| 413 | |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 414 | def process_rawq(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 415 | """Transfer from raw queue to cooked queue. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 416 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 417 | Set self.eof when connection is closed. Don't block unless in |
| 418 | the midst of an IAC sequence. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 420 | """ |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 421 | buf = [b'', b''] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 422 | try: |
| 423 | while self.rawq: |
| 424 | c = self.rawq_getchar() |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 425 | if not self.iacseq: |
| 426 | if c == theNULL: |
| 427 | continue |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 428 | if c == b"\021": |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 429 | continue |
| 430 | if c != IAC: |
| 431 | buf[self.sb] = buf[self.sb] + c |
| 432 | continue |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 433 | else: |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 434 | self.iacseq += c |
| 435 | elif len(self.iacseq) == 1: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 436 | # 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 437 | if c in (DO, DONT, WILL, WONT): |
| 438 | self.iacseq += c |
| 439 | continue |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 440 | |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 441 | self.iacseq = b'' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 442 | if c == IAC: |
| 443 | buf[self.sb] = buf[self.sb] + c |
Martin v. Löwis | b0162f9 | 2001-09-06 08:51:38 +0000 | [diff] [blame] | 444 | else: |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 445 | if c == SB: # SB ... SE start. |
| 446 | self.sb = 1 |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 447 | self.sbdataq = b'' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 448 | elif c == SE: |
| 449 | self.sb = 0 |
| 450 | self.sbdataq = self.sbdataq + buf[1] |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 451 | buf[1] = b'' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 452 | if self.option_callback: |
| 453 | # Callback is supposed to look into |
| 454 | # the sbdataq |
| 455 | self.option_callback(self.sock, c, NOOPT) |
| 456 | else: |
| 457 | # We can't offer automatic processing of |
| 458 | # suboptions. Alas, we should not get any |
| 459 | # unless we did a WILL/DO before. |
| 460 | self.msg('IAC %d not recognized' % ord(c)) |
| 461 | elif len(self.iacseq) == 2: |
| 462 | cmd = self.iacseq[1] |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 463 | self.iacseq = b'' |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 464 | opt = c |
| 465 | if cmd in (DO, DONT): |
Tim Peters | 230a60c | 2002-11-09 05:08:07 +0000 | [diff] [blame] | 466 | self.msg('IAC %s %d', |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 467 | cmd == DO and 'DO' or 'DONT', ord(opt)) |
| 468 | if self.option_callback: |
| 469 | self.option_callback(self.sock, cmd, opt) |
| 470 | else: |
| 471 | self.sock.sendall(IAC + WONT + opt) |
| 472 | elif cmd in (WILL, WONT): |
| 473 | self.msg('IAC %s %d', |
| 474 | cmd == WILL and 'WILL' or 'WONT', ord(opt)) |
| 475 | if self.option_callback: |
| 476 | self.option_callback(self.sock, cmd, opt) |
| 477 | else: |
| 478 | self.sock.sendall(IAC + DONT + opt) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 479 | except EOFError: # raised by self.rawq_getchar() |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 480 | self.iacseq = b'' # Reset on EOF |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 481 | self.sb = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 482 | pass |
Martin v. Löwis | 1da9c57 | 2002-11-04 09:56:00 +0000 | [diff] [blame] | 483 | self.cookedq = self.cookedq + buf[0] |
| 484 | self.sbdataq = self.sbdataq + buf[1] |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 485 | |
| 486 | def rawq_getchar(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 487 | """Get next char from raw queue. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 488 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 489 | Block if no data is immediately available. Raise EOFError |
| 490 | when connection is closed. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 491 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 492 | """ |
| 493 | if not self.rawq: |
| 494 | self.fill_rawq() |
| 495 | if self.eof: |
| 496 | raise EOFError |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 497 | c = self.rawq[self.irawq:self.irawq+1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 498 | self.irawq = self.irawq + 1 |
| 499 | if self.irawq >= len(self.rawq): |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 500 | self.rawq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 501 | self.irawq = 0 |
| 502 | return c |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 503 | |
| 504 | def fill_rawq(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 505 | """Fill raw queue from exactly one recv() system call. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 507 | Block if no data is immediately available. Set self.eof when |
| 508 | connection is closed. |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 509 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 510 | """ |
| 511 | if self.irawq >= len(self.rawq): |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 512 | self.rawq = b'' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 513 | self.irawq = 0 |
| 514 | # The buffer size should be fairly small so as to avoid quadratic |
| 515 | # behavior in process_rawq() above |
| 516 | buf = self.sock.recv(50) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 517 | self.msg("recv %r", buf) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 518 | self.eof = (not buf) |
| 519 | self.rawq = self.rawq + buf |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 520 | |
| 521 | def sock_avail(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 522 | """Test whether data is available on the socket.""" |
| 523 | return select.select([self], [], [], 0) == ([self], [], []) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 524 | |
| 525 | def interact(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 526 | """Interaction function, emulates a very dumb telnet client.""" |
Guido van Rossum | 82eae9e | 1998-12-23 23:04:17 +0000 | [diff] [blame] | 527 | if sys.platform == "win32": |
| 528 | self.mt_interact() |
| 529 | return |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 530 | while 1: |
| 531 | rfd, wfd, xfd = select.select([self, sys.stdin], [], []) |
| 532 | if self in rfd: |
| 533 | try: |
| 534 | text = self.read_eager() |
| 535 | except EOFError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 536 | print('*** Connection closed by remote host ***') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 537 | break |
| 538 | if text: |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 539 | sys.stdout.write(text.decode('ascii')) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 540 | sys.stdout.flush() |
| 541 | if sys.stdin in rfd: |
Benjamin Peterson | 3de7fb8 | 2008-10-15 20:54:24 +0000 | [diff] [blame] | 542 | line = sys.stdin.readline().encode('ascii') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 543 | if not line: |
| 544 | break |
| 545 | self.write(line) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 82eae9e | 1998-12-23 23:04:17 +0000 | [diff] [blame] | 547 | def mt_interact(self): |
| 548 | """Multithreaded version of interact().""" |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 549 | import _thread |
| 550 | _thread.start_new_thread(self.listener, ()) |
Guido van Rossum | 82eae9e | 1998-12-23 23:04:17 +0000 | [diff] [blame] | 551 | while 1: |
| 552 | line = sys.stdin.readline() |
| 553 | if not line: |
| 554 | break |
| 555 | self.write(line) |
| 556 | |
| 557 | def listener(self): |
| 558 | """Helper for mt_interact() -- this executes in the other thread.""" |
| 559 | while 1: |
| 560 | try: |
| 561 | data = self.read_eager() |
| 562 | except EOFError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 563 | print('*** Connection closed by remote host ***') |
Guido van Rossum | 82eae9e | 1998-12-23 23:04:17 +0000 | [diff] [blame] | 564 | return |
| 565 | if data: |
| 566 | sys.stdout.write(data) |
| 567 | else: |
| 568 | sys.stdout.flush() |
| 569 | |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 570 | def expect(self, list, timeout=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 571 | """Read until one from a list of a regular expressions matches. |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 572 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 573 | The first argument is a list of regular expressions, either |
| 574 | compiled (re.RegexObject instances) or uncompiled (strings). |
| 575 | The optional second argument is a timeout, in seconds; default |
| 576 | is no timeout. |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 578 | Return a tuple of three items: the index in the list of the |
| 579 | first regular expression that matches; the match object |
| 580 | returned; and the text read up till and including the match. |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 582 | If EOF is read and no text was read, raise EOFError. |
| 583 | Otherwise, when nothing matches, return (-1, None, text) where |
| 584 | text is the text received so far (may be the empty string if a |
| 585 | timeout happened). |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 586 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 587 | If a regular expression ends with a greedy match (e.g. '.*') |
| 588 | or if more than one expression can match the same input, the |
| 589 | results are undeterministic, and may depend on the I/O timing. |
Guido van Rossum | ccb5ec6 | 1997-12-24 22:24:19 +0000 | [diff] [blame] | 590 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 591 | """ |
| 592 | re = None |
| 593 | list = list[:] |
| 594 | indices = range(len(list)) |
| 595 | for i in indices: |
| 596 | if not hasattr(list[i], "search"): |
| 597 | if not re: import re |
| 598 | list[i] = re.compile(list[i]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 599 | if timeout is not None: |
| 600 | from time import time |
| 601 | time_start = time() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 602 | while 1: |
| 603 | self.process_rawq() |
| 604 | for i in indices: |
| 605 | m = list[i].search(self.cookedq) |
| 606 | if m: |
| 607 | e = m.end() |
| 608 | text = self.cookedq[:e] |
| 609 | self.cookedq = self.cookedq[e:] |
| 610 | return (i, m, text) |
| 611 | if self.eof: |
| 612 | break |
| 613 | if timeout is not None: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 614 | elapsed = time() - time_start |
| 615 | if elapsed >= timeout: |
| 616 | break |
| 617 | s_args = ([self.fileno()], [], [], timeout-elapsed) |
| 618 | r, w, x = select.select(*s_args) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 619 | if not r: |
| 620 | break |
| 621 | self.fill_rawq() |
| 622 | text = self.read_very_lazy() |
| 623 | if not text and self.eof: |
| 624 | raise EOFError |
| 625 | return (-1, None, text) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 626 | |
| 627 | |
| 628 | def test(): |
| 629 | """Test program for telnetlib. |
| 630 | |
| 631 | Usage: python telnetlib.py [-d] ... [host [port]] |
| 632 | |
| 633 | Default host is localhost; default port is 23. |
| 634 | |
| 635 | """ |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 636 | debuglevel = 0 |
| 637 | while sys.argv[1:] and sys.argv[1] == '-d': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 638 | debuglevel = debuglevel+1 |
| 639 | del sys.argv[1] |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 640 | host = 'localhost' |
| 641 | if sys.argv[1:]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 642 | host = sys.argv[1] |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 643 | port = 0 |
| 644 | if sys.argv[2:]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 645 | portstr = sys.argv[2] |
| 646 | try: |
| 647 | port = int(portstr) |
| 648 | except ValueError: |
| 649 | port = socket.getservbyname(portstr, 'tcp') |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 650 | tn = Telnet() |
| 651 | tn.set_debuglevel(debuglevel) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 652 | tn.open(host, port, timeout=0.5) |
Guido van Rossum | b9b50eb | 1997-12-24 21:07:04 +0000 | [diff] [blame] | 653 | tn.interact() |
| 654 | tn.close() |
| 655 | |
| 656 | if __name__ == '__main__': |
| 657 | test() |