c = self.rawq[self.irawq:self.irawq+1]
self.irawq = self.irawq + 1
if self.irawq >= len(self.rawq):
"""Fill raw queue from exactly one recv() system call.
Block if no data is immediately available. Set self.eof when
if self.irawq >= len(self.rawq):
# The buffer size should be fairly small so as to avoid quadratic
# behavior in process_rawq() above
self.rawq = self.rawq + buf
"""Test whether data is available on the socket."""
with _TelnetSelector() as selector:
selector.register(self, selectors.EVENT_READ)
return bool(selector.select(0))
"""Interaction function, emulates a very dumb telnet client."""
if sys.platform == "win32":
with _TelnetSelector() as selector:
selector.register(self, selectors.EVENT_READ)
selector.register(sys.stdin, selectors.EVENT_READ)
for key, events in selector.select():
print('*** Connection closed by remote host ***')
sys.stdout.write(text.decode('ascii'))
elif key.fileobj is sys.stdin:
line = sys.stdin.readline().encode('ascii')
"""Multithreaded version of interact()."""
_thread.start_new_thread(self.listener, ())
line = sys.stdin.readline()
self.write(line.encode('ascii'))
"""Helper for mt_interact() -- this executes in the other thread."""
print('*** Connection closed by remote host ***')
sys.stdout.write(data.decode('ascii'))
def expect(self, list, timeout=None):
"""Read until one from a list of a regular expressions matches.
The first argument is a list of regular expressions, either
compiled (re.Pattern instances) or uncompiled (strings).
The optional second argument is a timeout, in seconds; default
Return a tuple of three items: the index in the list of the
first regular expression that matches; the re.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
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.
indices = range(len(list))
if not hasattr(list[i], "search"):
list[i] = re.compile(list[i])
deadline = _time() + timeout
with _TelnetSelector() as selector:
selector.register(self, selectors.EVENT_READ)
m = list[i].search(self.cookedq)
self.cookedq = self.cookedq[e:]
ready = selector.select(timeout)
timeout = deadline - _time()
text = self.read_very_lazy()
if not text and self.eof:
def __exit__(self, type, value, traceback):
"""Test program for telnetlib.
Usage: python telnetlib.py [-d] ... [host [port]]
Default host is localhost; default port is 23.
while sys.argv[1:] and sys.argv[1] == '-d':
debuglevel = debuglevel+1
port = socket.getservbyname(portstr, 'tcp')
tn.set_debuglevel(debuglevel)
tn.open(host, port, timeout=0.5)
if __name__ == '__main__':