Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82eae9e

Browse files
committed
Added mt_interact() -- multithreaded version of interact().
interact() automatically uses this on Windows (where the single-threaded version doesn't work).
1 parent db01ee0 commit 82eae9e

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lib/telnetlib.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ def sock_avail(self):
376376

377377
def interact(self):
378378
"""Interaction function, emulates a very dumb telnet client."""
379+
if sys.platform == "win32":
380+
self.mt_interact()
381+
return
379382
while 1:
380383
rfd, wfd, xfd = select.select([self, sys.stdin], [], [])
381384
if self in rfd:
@@ -393,6 +396,29 @@ def interact(self):
393396
break
394397
self.write(line)
395398

399+
def mt_interact(self):
400+
"""Multithreaded version of interact()."""
401+
import thread
402+
thread.start_new_thread(self.listener, ())
403+
while 1:
404+
line = sys.stdin.readline()
405+
if not line:
406+
break
407+
self.write(line)
408+
409+
def listener(self):
410+
"""Helper for mt_interact() -- this executes in the other thread."""
411+
while 1:
412+
try:
413+
data = self.read_eager()
414+
except EOFError:
415+
print '*** Connection closed by remote host ***'
416+
return
417+
if data:
418+
sys.stdout.write(data)
419+
else:
420+
sys.stdout.flush()
421+
396422
def expect(self, list, timeout=None):
397423
"""Read until one from a list of a regular expressions matches.
398424

0 commit comments

Comments
 (0)