def _xinteract(tel): import selectors with telnetlib._TelnetSelector() as selector: selector.register(tel, selectors.EVENT_READ) selector.register(sys.stdin, selectors.EVENT_READ) while True: for key, events in selector.select(): if key.fileobj is tel: try: text = tel.read_eager() except EOFError: print('*** Connection closed by remote host ***') return if text: string = text.decode('ascii', 'backslashreplace') sys.stdout.write(string) sys.stdout.flush() elif key.fileobj is sys.stdin: line = sys.stdin.readline().encode('ascii') if not line: return tel.write(line)
def mud_interact(self): if sys.platform == "win32": self.tn.mt_interact() return with telnetlib._TelnetSelector() as selector: selector.register(self.tn, selectors.EVENT_READ) selector.register(sys.stdin, selectors.EVENT_READ) while True: for key, events in selector.select(): if key.fileobj is self.tn: try: text = self.tn.read_eager() except EOFError: print('*** Connection closed by remote host ***') return if text: self.receive(text) elif key.fileobj is sys.stdin: line = sys.stdin.readline().encode('ascii') if not line: return self.send(line)
def expect(self, regex, timeout=None): timeout = timeout or self.timeout if not isinstance(regex, list): regex = [regex] indices = range(len(regex)) for i in indices: if not hasattr(regex[i], "search"): regex[i] = re.compile(regex[i]) if timeout is not None: deadline = _time() + timeout with _TelnetSelector() as selector: selector.register(self, selectors.EVENT_READ) while not self.eof: self.process_rawq() for i in indices: m = regex[i].search(self.cookedq.decode( 'utf-8', 'replace')) if m: e = m.end() text = self.cookedq[:e] self.cookedq = self.cookedq[e:] self.log(text) return (i, m, text) if timeout is not None: ready = selector.select(timeout) timeout = deadline - _time() if not ready: if timeout < 0: break else: continue self.fill_rawq() text = self.read_very_lazy() if not text and self.eof: raise EOFError self.log(text) return (-1, None, text)