|
70 | 70 | import time |
71 | 71 | import socket |
72 | 72 | import select |
| 73 | +import selectors |
| 74 | +from time import monotonic as _time |
73 | 75 | import struct |
74 | 76 | from io import StringIO |
75 | 77 |
|
|
155 | 157 |
|
156 | 158 | SEND_TTYPE = chr(1).encode('latin-1') |
157 | 159 |
|
| 160 | +# poll/select have the advantage of not requiring any extra file descriptor, |
| 161 | +# contrarily to epoll/kqueue (also, they require a single syscall). |
| 162 | +if hasattr(selectors, 'PollSelector'): |
| 163 | + _TelnetSelector = selectors.PollSelector |
| 164 | +else: |
| 165 | + _TelnetSelector = selectors.SelectSelector |
158 | 166 |
|
159 | 167 | class Telnet(object): |
160 | 168 |
|
@@ -322,26 +330,26 @@ def read_until(self, match, timeout=None): |
322 | 330 | """ |
323 | 331 | n = len(match) |
324 | 332 | self.process_rawq() |
325 | | - i = self.cookedq.find(match) |
| 333 | + i = self.rawq.find(match) |
326 | 334 | if i >= 0: |
327 | 335 | i = i+n |
328 | | - buf = self.cookedq[:i] |
329 | | - self.cookedq = self.cookedq[i:] |
| 336 | + buf = self.rawq[:i] |
| 337 | + self.rawq = self.rawq[i:] |
330 | 338 | return buf |
331 | 339 | if timeout is not None: |
332 | 340 | deadline = _time() + timeout |
333 | 341 | with _TelnetSelector() as selector: |
334 | 342 | selector.register(self, selectors.EVENT_READ) |
335 | 343 | while not self.eof: |
336 | 344 | if selector.select(timeout): |
337 | | - i = max(0, len(self.cookedq)-n) |
| 345 | + i = max(0, len(self.rawq)-n) |
338 | 346 | self.fill_rawq() |
339 | 347 | self.process_rawq() |
340 | | - i = self.cookedq.find(match, i) |
| 348 | + i = self.rawq.find(match, i) |
341 | 349 | if i >= 0: |
342 | 350 | i = i+n |
343 | | - buf = self.cookedq[:i] |
344 | | - self.cookedq = self.cookedq[i:] |
| 351 | + buf = self.rawq[:i] |
| 352 | + self.rawq = self.rawq[i:] |
345 | 353 | return buf |
346 | 354 | if timeout is not None: |
347 | 355 | timeout = deadline - _time() |
|
0 commit comments