| 
									
										
										
										
											2005-09-07 07:40:05 +00:00
										 |  |  | r"""TELNET client class.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Based on RFC 854: TELNET Protocol Specification, by J. Postel and | 
					
						
							|  |  |  | J. Reynolds | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | >>> from telnetlib import Telnet | 
					
						
							|  |  |  | >>> tn = Telnet('www.python.org', 79)   # connect to finger port | 
					
						
							|  |  |  | >>> tn.write('guido\r\n') | 
					
						
							|  |  |  | >>> print tn.read_all() | 
					
						
							|  |  |  | Login       Name               TTY         Idle    When    Where | 
					
						
							|  |  |  | guido    Guido van Rossum      pts/2        <Dec  2 11:10> snag.cnri.reston.. | 
					
						
							| 
									
										
										
										
											2001-01-15 03:26:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | >>> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that read_all() won't read until eof -- it just reads some data | 
					
						
							|  |  |  | -- but it guarantees to read at least one byte unless EOF is hit. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | It is possible to pass a Telnet object to select.select() in order to | 
					
						
							|  |  |  | wait until more data is available.  Note that in this case, | 
					
						
							|  |  |  | read_eager() may return '' even if there was data on the socket, | 
					
						
							|  |  |  | because the protocol negotiation may have eaten the data.  This is why | 
					
						
							|  |  |  | EOFError is needed in some cases to distinguish between "no data" and | 
					
						
							|  |  |  | "connection closed" (since the socket also appears ready for reading | 
					
						
							|  |  |  | when it is closed). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To do: | 
					
						
							|  |  |  | - option negotiation | 
					
						
							| 
									
										
										
										
											1997-12-24 22:24:19 +00:00
										 |  |  | - timeout should be intrinsic to the connection object instead of an | 
					
						
							|  |  |  |   option on one of the read calls only | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Imported modules | 
					
						
							| 
									
										
										
										
											1997-12-24 22:24:19 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | import socket | 
					
						
							|  |  |  | import select | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-01 04:27:19 +00:00
										 |  |  | __all__ = ["Telnet"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | # Tunable parameters | 
					
						
							|  |  |  | DEBUGLEVEL = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Telnet protocol defaults | 
					
						
							|  |  |  | TELNET_PORT = 23 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Telnet protocol characters (don't change) | 
					
						
							|  |  |  | IAC  = chr(255) # "Interpret As Command" | 
					
						
							|  |  |  | DONT = chr(254) | 
					
						
							|  |  |  | DO   = chr(253) | 
					
						
							|  |  |  | WONT = chr(252) | 
					
						
							|  |  |  | WILL = chr(251) | 
					
						
							|  |  |  | theNULL = chr(0) | 
					
						
							| 
									
										
										
										
											2002-11-04 17:34:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | SE  = chr(240)  # Subnegotiation End | 
					
						
							|  |  |  | NOP = chr(241)  # No Operation | 
					
						
							|  |  |  | DM  = chr(242)  # Data Mark | 
					
						
							|  |  |  | BRK = chr(243)  # Break | 
					
						
							|  |  |  | IP  = chr(244)  # Interrupt process | 
					
						
							|  |  |  | AO  = chr(245)  # Abort output | 
					
						
							|  |  |  | AYT = chr(246)  # Are You There | 
					
						
							|  |  |  | EC  = chr(247)  # Erase Character | 
					
						
							|  |  |  | EL  = chr(248)  # Erase Line | 
					
						
							|  |  |  | GA  = chr(249)  # Go Ahead | 
					
						
							|  |  |  | SB =  chr(250)  # Subnegotiation Begin | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  | # Telnet protocol options code (don't change) | 
					
						
							|  |  |  | # These ones all come from arpa/telnet.h | 
					
						
							|  |  |  | BINARY = chr(0) # 8-bit data path | 
					
						
							|  |  |  | ECHO = chr(1) # echo | 
					
						
							|  |  |  | RCP = chr(2) # prepare to reconnect | 
					
						
							|  |  |  | SGA = chr(3) # suppress go ahead | 
					
						
							|  |  |  | NAMS = chr(4) # approximate message size | 
					
						
							|  |  |  | STATUS = chr(5) # give status | 
					
						
							|  |  |  | TM = chr(6) # timing mark | 
					
						
							|  |  |  | RCTE = chr(7) # remote controlled transmission and echo | 
					
						
							|  |  |  | NAOL = chr(8) # negotiate about output line width | 
					
						
							|  |  |  | NAOP = chr(9) # negotiate about output page size | 
					
						
							|  |  |  | NAOCRD = chr(10) # negotiate about CR disposition | 
					
						
							|  |  |  | NAOHTS = chr(11) # negotiate about horizontal tabstops | 
					
						
							|  |  |  | NAOHTD = chr(12) # negotiate about horizontal tab disposition | 
					
						
							|  |  |  | NAOFFD = chr(13) # negotiate about formfeed disposition | 
					
						
							|  |  |  | NAOVTS = chr(14) # negotiate about vertical tab stops | 
					
						
							|  |  |  | NAOVTD = chr(15) # negotiate about vertical tab disposition | 
					
						
							|  |  |  | NAOLFD = chr(16) # negotiate about output LF disposition | 
					
						
							|  |  |  | XASCII = chr(17) # extended ascii character set | 
					
						
							|  |  |  | LOGOUT = chr(18) # force logout | 
					
						
							|  |  |  | BM = chr(19) # byte macro | 
					
						
							|  |  |  | DET = chr(20) # data entry terminal | 
					
						
							|  |  |  | SUPDUP = chr(21) # supdup protocol | 
					
						
							|  |  |  | SUPDUPOUTPUT = chr(22) # supdup output | 
					
						
							|  |  |  | SNDLOC = chr(23) # send location | 
					
						
							|  |  |  | TTYPE = chr(24) # terminal type | 
					
						
							|  |  |  | EOR = chr(25) # end or record | 
					
						
							|  |  |  | TUID = chr(26) # TACACS user identification | 
					
						
							|  |  |  | OUTMRK = chr(27) # output marking | 
					
						
							|  |  |  | TTYLOC = chr(28) # terminal location number | 
					
						
							|  |  |  | VT3270REGIME = chr(29) # 3270 regime | 
					
						
							|  |  |  | X3PAD = chr(30) # X.3 PAD | 
					
						
							|  |  |  | NAWS = chr(31) # window size | 
					
						
							|  |  |  | TSPEED = chr(32) # terminal speed | 
					
						
							|  |  |  | LFLOW = chr(33) # remote flow control | 
					
						
							|  |  |  | LINEMODE = chr(34) # Linemode option | 
					
						
							|  |  |  | XDISPLOC = chr(35) # X Display Location | 
					
						
							|  |  |  | OLD_ENVIRON = chr(36) # Old - Environment variables | 
					
						
							|  |  |  | AUTHENTICATION = chr(37) # Authenticate | 
					
						
							|  |  |  | ENCRYPT = chr(38) # Encryption option | 
					
						
							|  |  |  | NEW_ENVIRON = chr(39) # New - Environment variables | 
					
						
							|  |  |  | # the following ones come from | 
					
						
							|  |  |  | # http://www.iana.org/assignments/telnet-options | 
					
						
							|  |  |  | # Unfortunately, that document does not assign identifiers | 
					
						
							|  |  |  | # to all of them, so we are making them up | 
					
						
							|  |  |  | TN3270E = chr(40) # TN3270E | 
					
						
							|  |  |  | XAUTH = chr(41) # XAUTH | 
					
						
							|  |  |  | CHARSET = chr(42) # CHARSET | 
					
						
							|  |  |  | RSP = chr(43) # Telnet Remote Serial Port | 
					
						
							|  |  |  | COM_PORT_OPTION = chr(44) # Com Port Control Option | 
					
						
							|  |  |  | SUPPRESS_LOCAL_ECHO = chr(45) # Telnet Suppress Local Echo | 
					
						
							|  |  |  | TLS = chr(46) # Telnet Start TLS | 
					
						
							|  |  |  | KERMIT = chr(47) # KERMIT | 
					
						
							|  |  |  | SEND_URL = chr(48) # SEND-URL | 
					
						
							|  |  |  | FORWARD_X = chr(49) # FORWARD_X | 
					
						
							|  |  |  | PRAGMA_LOGON = chr(138) # TELOPT PRAGMA LOGON | 
					
						
							|  |  |  | SSPI_LOGON = chr(139) # TELOPT SSPI LOGON | 
					
						
							|  |  |  | PRAGMA_HEARTBEAT = chr(140) # TELOPT PRAGMA HEARTBEAT | 
					
						
							|  |  |  | EXOPL = chr(255) # Extended-Options-List | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  | NOOPT = chr(0) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Telnet: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Telnet interface class.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     An instance of this class represents a connection to a telnet | 
					
						
							|  |  |  |     server.  The instance is initially not connected; the open() | 
					
						
							|  |  |  |     method must be used to establish a connection.  Alternatively, the | 
					
						
							|  |  |  |     host name and optional port number can be passed to the | 
					
						
							|  |  |  |     constructor, too. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Don't try to reopen an already connected instance. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This class has many read_*() methods.  Note that some of them | 
					
						
							|  |  |  |     raise EOFError when the end of the connection is read, because | 
					
						
							|  |  |  |     they can return an empty string for other reasons.  See the | 
					
						
							|  |  |  |     individual doc strings. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     read_until(expected, [timeout]) | 
					
						
							|  |  |  |         Read until the expected string has been seen, or a timeout is | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         hit (default is no timeout); may block. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     read_all() | 
					
						
							|  |  |  |         Read all data until EOF; may block. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     read_some() | 
					
						
							|  |  |  |         Read at least one byte or EOF; may block. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     read_very_eager() | 
					
						
							|  |  |  |         Read all data available already queued or on the socket, | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         without blocking. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     read_eager() | 
					
						
							|  |  |  |         Read either data already queued or some data available on the | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         socket, without blocking. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     read_lazy() | 
					
						
							|  |  |  |         Read all data in the raw queue (processing it first), without | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         doing any socket I/O. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     read_very_lazy() | 
					
						
							|  |  |  |         Reads all data in the cooked queue, without doing any socket | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         I/O. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |     read_sb_data() | 
					
						
							|  |  |  |         Reads available data between SB ... SE sequence. Don't block. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  |     set_option_negotiation_callback(callback) | 
					
						
							|  |  |  |         Each time a telnet option is read on the input flow, this callback | 
					
						
							|  |  |  |         (if set) is called with the following parameters : | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |         callback(telnet socket, command, option) | 
					
						
							|  |  |  |             option will be chr(0) when there is no option. | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  |         No other action is done afterwards by telnetlib. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-29 18:22:35 +00:00
										 |  |  |     def __init__(self, host=None, port=0, timeout=None): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Constructor.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         When called without arguments, create an unconnected instance. | 
					
						
							|  |  |  |         With a hostname argument, it connects the instance; a port | 
					
						
							|  |  |  |         number is optional. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         self.debuglevel = DEBUGLEVEL | 
					
						
							|  |  |  |         self.host = host | 
					
						
							|  |  |  |         self.port = port | 
					
						
							| 
									
										
										
										
											2007-03-29 18:22:35 +00:00
										 |  |  |         self.timeout = timeout | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         self.sock = None | 
					
						
							|  |  |  |         self.rawq = '' | 
					
						
							|  |  |  |         self.irawq = 0 | 
					
						
							|  |  |  |         self.cookedq = '' | 
					
						
							|  |  |  |         self.eof = 0 | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |         self.iacseq = '' # Buffer for IAC sequence. | 
					
						
							|  |  |  |         self.sb = 0 # flag for SB and SE sequence. | 
					
						
							|  |  |  |         self.sbdataq = '' | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  |         self.option_callback = None | 
					
						
							| 
									
										
										
										
											2002-06-02 03:04:52 +00:00
										 |  |  |         if host is not None: | 
					
						
							| 
									
										
										
										
											2007-03-29 18:22:35 +00:00
										 |  |  |             self.open(host, port, timeout) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-29 18:22:35 +00:00
										 |  |  |     def open(self, host, port=0, timeout=None): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Connect to a host.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         The optional second argument is the port number, which | 
					
						
							|  |  |  |         defaults to the standard telnet port (23). | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Don't try to reopen an already connected instance. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self.eof = 0 | 
					
						
							|  |  |  |         if not port: | 
					
						
							|  |  |  |             port = TELNET_PORT | 
					
						
							|  |  |  |         self.host = host | 
					
						
							|  |  |  |         self.port = port | 
					
						
							| 
									
										
										
										
											2007-03-29 18:22:35 +00:00
										 |  |  |         if timeout is not None: | 
					
						
							|  |  |  |             self.timeout = timeout | 
					
						
							|  |  |  |         self.sock = socket.create_connection((host, port), self.timeout) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __del__(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Destructor -- close the connection.""" | 
					
						
							|  |  |  |         self.close() | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def msg(self, msg, *args): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Print a debug message, when the debug level is > 0.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         If extra arguments are present, they are substituted in the | 
					
						
							|  |  |  |         message using the standard string formatting operator. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         if self.debuglevel > 0: | 
					
						
							|  |  |  |             print 'Telnet(%s,%d):' % (self.host, self.port), | 
					
						
							|  |  |  |             if args: | 
					
						
							|  |  |  |                 print msg % args | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 print msg | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def set_debuglevel(self, debuglevel): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Set the debug level.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         The higher it is, the more debug output you get (on sys.stdout). | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self.debuglevel = debuglevel | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Close the connection.""" | 
					
						
							|  |  |  |         if self.sock: | 
					
						
							|  |  |  |             self.sock.close() | 
					
						
							|  |  |  |         self.sock = 0 | 
					
						
							|  |  |  |         self.eof = 1 | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |         self.iacseq = '' | 
					
						
							|  |  |  |         self.sb = 0 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_socket(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Return the socket object used internally.""" | 
					
						
							|  |  |  |         return self.sock | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def fileno(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Return the fileno() of the socket object used internally.""" | 
					
						
							|  |  |  |         return self.sock.fileno() | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def write(self, buffer): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Write a string to the socket, doubling any IAC characters.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Can block if the connection is blocked.  May raise | 
					
						
							|  |  |  |         socket.error if the connection is closed. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         if IAC in buffer: | 
					
						
							| 
									
										
										
										
											2001-02-09 07:10:12 +00:00
										 |  |  |             buffer = buffer.replace(IAC, IAC+IAC) | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         self.msg("send %r", buffer) | 
					
						
							| 
									
										
										
										
											2002-02-16 23:06:19 +00:00
										 |  |  |         self.sock.sendall(buffer) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_until(self, match, timeout=None): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Read until a given string is encountered or until timeout.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         When no match is found, return whatever is available instead, | 
					
						
							|  |  |  |         possibly the empty string.  Raise EOFError if the connection | 
					
						
							|  |  |  |         is closed and no cooked data is available. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         n = len(match) | 
					
						
							|  |  |  |         self.process_rawq() | 
					
						
							| 
									
										
										
										
											2001-02-09 07:10:12 +00:00
										 |  |  |         i = self.cookedq.find(match) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         if i >= 0: | 
					
						
							|  |  |  |             i = i+n | 
					
						
							|  |  |  |             buf = self.cookedq[:i] | 
					
						
							|  |  |  |             self.cookedq = self.cookedq[i:] | 
					
						
							|  |  |  |             return buf | 
					
						
							|  |  |  |         s_reply = ([self], [], []) | 
					
						
							|  |  |  |         s_args = s_reply | 
					
						
							|  |  |  |         if timeout is not None: | 
					
						
							|  |  |  |             s_args = s_args + (timeout,) | 
					
						
							| 
									
										
										
										
											2006-07-03 13:01:35 +00:00
										 |  |  |             from time import time | 
					
						
							|  |  |  |             time_start = time() | 
					
						
							| 
									
										
										
										
											2003-02-27 20:14:51 +00:00
										 |  |  |         while not self.eof and select.select(*s_args) == s_reply: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             i = max(0, len(self.cookedq)-n) | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |             self.process_rawq() | 
					
						
							| 
									
										
										
										
											2001-02-09 07:10:12 +00:00
										 |  |  |             i = self.cookedq.find(match, i) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             if i >= 0: | 
					
						
							|  |  |  |                 i = i+n | 
					
						
							|  |  |  |                 buf = self.cookedq[:i] | 
					
						
							|  |  |  |                 self.cookedq = self.cookedq[i:] | 
					
						
							|  |  |  |                 return buf | 
					
						
							| 
									
										
										
										
											2006-07-03 13:01:35 +00:00
										 |  |  |             if timeout is not None: | 
					
						
							|  |  |  |                 elapsed = time() - time_start | 
					
						
							|  |  |  |                 if elapsed >= timeout: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 s_args = s_reply + (timeout-elapsed,) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         return self.read_very_lazy() | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_all(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Read all data until EOF; block until connection closed.""" | 
					
						
							|  |  |  |         self.process_rawq() | 
					
						
							|  |  |  |         while not self.eof: | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |             self.process_rawq() | 
					
						
							|  |  |  |         buf = self.cookedq | 
					
						
							|  |  |  |         self.cookedq = '' | 
					
						
							|  |  |  |         return buf | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_some(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Read at least one byte of cooked data unless EOF is hit.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Return '' if EOF is hit.  Block if no data is immediately | 
					
						
							|  |  |  |         available. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self.process_rawq() | 
					
						
							|  |  |  |         while not self.cookedq and not self.eof: | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |             self.process_rawq() | 
					
						
							|  |  |  |         buf = self.cookedq | 
					
						
							|  |  |  |         self.cookedq = '' | 
					
						
							|  |  |  |         return buf | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_very_eager(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Read everything that's possible without blocking in I/O (eager).
 | 
					
						
							| 
									
										
										
										
											2001-01-15 03:26:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Raise EOFError if connection closed and no cooked data | 
					
						
							|  |  |  |         available.  Return '' if no cooked data available otherwise. | 
					
						
							|  |  |  |         Don't block unless in the midst of an IAC sequence. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         self.process_rawq() | 
					
						
							|  |  |  |         while not self.eof and self.sock_avail(): | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |             self.process_rawq() | 
					
						
							|  |  |  |         return self.read_very_lazy() | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_eager(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Read readily available data.
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Raise EOFError if connection closed and no cooked data | 
					
						
							|  |  |  |         available.  Return '' if no cooked data available otherwise. | 
					
						
							|  |  |  |         Don't block unless in the midst of an IAC sequence. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self.process_rawq() | 
					
						
							|  |  |  |         while not self.cookedq and not self.eof and self.sock_avail(): | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |             self.process_rawq() | 
					
						
							|  |  |  |         return self.read_very_lazy() | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_lazy(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Process and return data that's already in the queues (lazy).
 | 
					
						
							| 
									
										
										
										
											2001-01-15 03:26:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Raise EOFError if connection closed and no data available. | 
					
						
							|  |  |  |         Return '' if no cooked data available otherwise.  Don't block | 
					
						
							|  |  |  |         unless in the midst of an IAC sequence. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self.process_rawq() | 
					
						
							|  |  |  |         return self.read_very_lazy() | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def read_very_lazy(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Return any data available in the cooked queue (very lazy).
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         Raise EOFError if connection closed and no data available. | 
					
						
							|  |  |  |         Return '' if no cooked data available otherwise.  Don't block. | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         buf = self.cookedq | 
					
						
							|  |  |  |         self.cookedq = '' | 
					
						
							|  |  |  |         if not buf and self.eof and not self.rawq: | 
					
						
							|  |  |  |             raise EOFError, 'telnet connection closed' | 
					
						
							|  |  |  |         return buf | 
					
						
							| 
									
										
										
										
											2002-11-09 05:08:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |     def read_sb_data(self): | 
					
						
							|  |  |  |         """Return any data available in the SB ... SE queue.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-09 05:08:07 +00:00
										 |  |  |         Return '' if no SB ... SE available. Should only be called | 
					
						
							|  |  |  |         after seeing a SB or SE command. When a new SB command is | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |         found, old unread SB data will be discarded. Don't block. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         buf = self.sbdataq | 
					
						
							|  |  |  |         self.sbdataq = '' | 
					
						
							|  |  |  |         return buf | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  |     def set_option_negotiation_callback(self, callback): | 
					
						
							|  |  |  |         """Provide a callback function called after each receipt of a telnet option.""" | 
					
						
							|  |  |  |         self.option_callback = callback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  |     def process_rawq(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Transfer from raw queue to cooked queue.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Set self.eof when connection is closed.  Don't block unless in | 
					
						
							|  |  |  |         the midst of an IAC sequence. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |         buf = ['', ''] | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             while self.rawq: | 
					
						
							|  |  |  |                 c = self.rawq_getchar() | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |                 if not self.iacseq: | 
					
						
							|  |  |  |                     if c == theNULL: | 
					
						
							|  |  |  |                         continue | 
					
						
							|  |  |  |                     if c == "\021": | 
					
						
							|  |  |  |                         continue | 
					
						
							|  |  |  |                     if c != IAC: | 
					
						
							|  |  |  |                         buf[self.sb] = buf[self.sb] + c | 
					
						
							|  |  |  |                         continue | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  |                     else: | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |                         self.iacseq += c | 
					
						
							|  |  |  |                 elif len(self.iacseq) == 1: | 
					
						
							| 
									
										
										
										
											2006-04-09 04:00:49 +00:00
										 |  |  |                     # 'IAC: IAC CMD [OPTION only for WILL/WONT/DO/DONT]' | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |                     if c in (DO, DONT, WILL, WONT): | 
					
						
							|  |  |  |                         self.iacseq += c | 
					
						
							|  |  |  |                         continue | 
					
						
							| 
									
										
										
										
											2002-11-09 05:08:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |                     self.iacseq = '' | 
					
						
							|  |  |  |                     if c == IAC: | 
					
						
							|  |  |  |                         buf[self.sb] = buf[self.sb] + c | 
					
						
							| 
									
										
										
										
											2001-09-06 08:51:38 +00:00
										 |  |  |                     else: | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |                         if c == SB: # SB ... SE start. | 
					
						
							|  |  |  |                             self.sb = 1 | 
					
						
							|  |  |  |                             self.sbdataq = '' | 
					
						
							|  |  |  |                         elif c == SE: | 
					
						
							|  |  |  |                             self.sb = 0 | 
					
						
							|  |  |  |                             self.sbdataq = self.sbdataq + buf[1] | 
					
						
							|  |  |  |                             buf[1] = '' | 
					
						
							|  |  |  |                         if self.option_callback: | 
					
						
							|  |  |  |                             # Callback is supposed to look into | 
					
						
							|  |  |  |                             # the sbdataq | 
					
						
							|  |  |  |                             self.option_callback(self.sock, c, NOOPT) | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             # We can't offer automatic processing of | 
					
						
							|  |  |  |                             # suboptions. Alas, we should not get any | 
					
						
							|  |  |  |                             # unless we did a WILL/DO before. | 
					
						
							|  |  |  |                             self.msg('IAC %d not recognized' % ord(c)) | 
					
						
							|  |  |  |                 elif len(self.iacseq) == 2: | 
					
						
							|  |  |  |                     cmd = self.iacseq[1] | 
					
						
							|  |  |  |                     self.iacseq = '' | 
					
						
							|  |  |  |                     opt = c | 
					
						
							|  |  |  |                     if cmd in (DO, DONT): | 
					
						
							| 
									
										
										
										
											2002-11-09 05:08:07 +00:00
										 |  |  |                         self.msg('IAC %s %d', | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |                             cmd == DO and 'DO' or 'DONT', ord(opt)) | 
					
						
							|  |  |  |                         if self.option_callback: | 
					
						
							|  |  |  |                             self.option_callback(self.sock, cmd, opt) | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             self.sock.sendall(IAC + WONT + opt) | 
					
						
							|  |  |  |                     elif cmd in (WILL, WONT): | 
					
						
							|  |  |  |                         self.msg('IAC %s %d', | 
					
						
							|  |  |  |                             cmd == WILL and 'WILL' or 'WONT', ord(opt)) | 
					
						
							|  |  |  |                         if self.option_callback: | 
					
						
							|  |  |  |                             self.option_callback(self.sock, cmd, opt) | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             self.sock.sendall(IAC + DONT + opt) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         except EOFError: # raised by self.rawq_getchar() | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |             self.iacseq = '' # Reset on EOF | 
					
						
							|  |  |  |             self.sb = 0 | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |             pass | 
					
						
							| 
									
										
										
										
											2002-11-04 09:56:00 +00:00
										 |  |  |         self.cookedq = self.cookedq + buf[0] | 
					
						
							|  |  |  |         self.sbdataq = self.sbdataq + buf[1] | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def rawq_getchar(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Get next char from raw queue.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Block if no data is immediately available.  Raise EOFError | 
					
						
							|  |  |  |         when connection is closed. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if not self.rawq: | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |             if self.eof: | 
					
						
							|  |  |  |                 raise EOFError | 
					
						
							|  |  |  |         c = self.rawq[self.irawq] | 
					
						
							|  |  |  |         self.irawq = self.irawq + 1 | 
					
						
							|  |  |  |         if self.irawq >= len(self.rawq): | 
					
						
							|  |  |  |             self.rawq = '' | 
					
						
							|  |  |  |             self.irawq = 0 | 
					
						
							|  |  |  |         return c | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def fill_rawq(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Fill raw queue from exactly one recv() system call.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Block if no data is immediately available.  Set self.eof when | 
					
						
							|  |  |  |         connection is closed. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if self.irawq >= len(self.rawq): | 
					
						
							|  |  |  |             self.rawq = '' | 
					
						
							|  |  |  |             self.irawq = 0 | 
					
						
							|  |  |  |         # The buffer size should be fairly small so as to avoid quadratic | 
					
						
							|  |  |  |         # behavior in process_rawq() above | 
					
						
							|  |  |  |         buf = self.sock.recv(50) | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         self.msg("recv %r", buf) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         self.eof = (not buf) | 
					
						
							|  |  |  |         self.rawq = self.rawq + buf | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def sock_avail(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Test whether data is available on the socket.""" | 
					
						
							|  |  |  |         return select.select([self], [], [], 0) == ([self], [], []) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def interact(self): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Interaction function, emulates a very dumb telnet client.""" | 
					
						
							| 
									
										
										
										
											1998-12-23 23:04:17 +00:00
										 |  |  |         if sys.platform == "win32": | 
					
						
							|  |  |  |             self.mt_interact() | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         while 1: | 
					
						
							|  |  |  |             rfd, wfd, xfd = select.select([self, sys.stdin], [], []) | 
					
						
							|  |  |  |             if self in rfd: | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     text = self.read_eager() | 
					
						
							|  |  |  |                 except EOFError: | 
					
						
							|  |  |  |                     print '*** Connection closed by remote host ***' | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 if text: | 
					
						
							|  |  |  |                     sys.stdout.write(text) | 
					
						
							|  |  |  |                     sys.stdout.flush() | 
					
						
							|  |  |  |             if sys.stdin in rfd: | 
					
						
							|  |  |  |                 line = sys.stdin.readline() | 
					
						
							|  |  |  |                 if not line: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 self.write(line) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-23 23:04:17 +00:00
										 |  |  |     def mt_interact(self): | 
					
						
							|  |  |  |         """Multithreaded version of interact().""" | 
					
						
							|  |  |  |         import thread | 
					
						
							|  |  |  |         thread.start_new_thread(self.listener, ()) | 
					
						
							|  |  |  |         while 1: | 
					
						
							|  |  |  |             line = sys.stdin.readline() | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             self.write(line) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def listener(self): | 
					
						
							|  |  |  |         """Helper for mt_interact() -- this executes in the other thread.""" | 
					
						
							|  |  |  |         while 1: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 data = self.read_eager() | 
					
						
							|  |  |  |             except EOFError: | 
					
						
							|  |  |  |                 print '*** Connection closed by remote host ***' | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             if data: | 
					
						
							|  |  |  |                 sys.stdout.write(data) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 sys.stdout.flush() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-24 22:24:19 +00:00
										 |  |  |     def expect(self, list, timeout=None): | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         """Read until one from a list of a regular expressions matches.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The first argument is a list of regular expressions, either | 
					
						
							|  |  |  |         compiled (re.RegexObject instances) or uncompiled (strings). | 
					
						
							|  |  |  |         The optional second argument is a timeout, in seconds; default | 
					
						
							|  |  |  |         is no timeout. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Return a tuple of three items: the index in the list of the | 
					
						
							|  |  |  |         first regular expression that matches; the match object | 
					
						
							|  |  |  |         returned; and the text read up till and including the match. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If EOF is read and no text was read, raise EOFError. | 
					
						
							|  |  |  |         Otherwise, when nothing matches, return (-1, None, text) where | 
					
						
							|  |  |  |         text is the text received so far (may be the empty string if a | 
					
						
							|  |  |  |         timeout happened). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If a regular expression ends with a greedy match (e.g. '.*') | 
					
						
							|  |  |  |         or if more than one expression can match the same input, the | 
					
						
							|  |  |  |         results are undeterministic, and may depend on the I/O timing. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         re = None | 
					
						
							|  |  |  |         list = list[:] | 
					
						
							|  |  |  |         indices = range(len(list)) | 
					
						
							|  |  |  |         for i in indices: | 
					
						
							|  |  |  |             if not hasattr(list[i], "search"): | 
					
						
							|  |  |  |                 if not re: import re | 
					
						
							|  |  |  |                 list[i] = re.compile(list[i]) | 
					
						
							| 
									
										
										
										
											2006-07-03 13:01:35 +00:00
										 |  |  |         if timeout is not None: | 
					
						
							|  |  |  |             from time import time | 
					
						
							|  |  |  |             time_start = time() | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         while 1: | 
					
						
							|  |  |  |             self.process_rawq() | 
					
						
							|  |  |  |             for i in indices: | 
					
						
							|  |  |  |                 m = list[i].search(self.cookedq) | 
					
						
							|  |  |  |                 if m: | 
					
						
							|  |  |  |                     e = m.end() | 
					
						
							|  |  |  |                     text = self.cookedq[:e] | 
					
						
							|  |  |  |                     self.cookedq = self.cookedq[e:] | 
					
						
							|  |  |  |                     return (i, m, text) | 
					
						
							|  |  |  |             if self.eof: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             if timeout is not None: | 
					
						
							| 
									
										
										
										
											2006-07-03 13:01:35 +00:00
										 |  |  |                 elapsed = time() - time_start | 
					
						
							|  |  |  |                 if elapsed >= timeout: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 s_args = ([self.fileno()], [], [], timeout-elapsed) | 
					
						
							|  |  |  |                 r, w, x = select.select(*s_args) | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |                 if not r: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |             self.fill_rawq() | 
					
						
							|  |  |  |         text = self.read_very_lazy() | 
					
						
							|  |  |  |         if not text and self.eof: | 
					
						
							|  |  |  |             raise EOFError | 
					
						
							|  |  |  |         return (-1, None, text) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(): | 
					
						
							|  |  |  |     """Test program for telnetlib.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Usage: python telnetlib.py [-d] ... [host [port]] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Default host is localhost; default port is 23. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     debuglevel = 0 | 
					
						
							|  |  |  |     while sys.argv[1:] and sys.argv[1] == '-d': | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         debuglevel = debuglevel+1 | 
					
						
							|  |  |  |         del sys.argv[1] | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  |     host = 'localhost' | 
					
						
							|  |  |  |     if sys.argv[1:]: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         host = sys.argv[1] | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  |     port = 0 | 
					
						
							|  |  |  |     if sys.argv[2:]: | 
					
						
							| 
									
										
										
										
											1998-03-26 21:13:24 +00:00
										 |  |  |         portstr = sys.argv[2] | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             port = int(portstr) | 
					
						
							|  |  |  |         except ValueError: | 
					
						
							|  |  |  |             port = socket.getservbyname(portstr, 'tcp') | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  |     tn = Telnet() | 
					
						
							|  |  |  |     tn.set_debuglevel(debuglevel) | 
					
						
							| 
									
										
										
										
											2007-03-29 18:22:35 +00:00
										 |  |  |     tn.open(host, port, timeout=0.5) | 
					
						
							| 
									
										
										
										
											1997-12-24 21:07:04 +00:00
										 |  |  |     tn.interact() | 
					
						
							|  |  |  |     tn.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test() |