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