Skip to content

Commit fe50f11

Browse files
Small fixes regarding previous commit about telnet changes.
- Fix issue when closing the telnet connection. - Handle `EOFError` and `KeyboardInterrupt` in telnet applications.
1 parent 97ac514 commit fe50f11

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

prompt_toolkit/contrib/telnet/server.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def isatty(self) -> bool:
9999

100100
def flush(self) -> None:
101101
try:
102-
self._connection.send(b"".join(self._buffer))
102+
if not self._closed:
103+
self._connection.send(b"".join(self._buffer))
103104
except OSError as e:
104105
logger.warning("Couldn't send data over socket: %s" % e)
105106

@@ -355,6 +356,15 @@ async def run() -> None:
355356
finally:
356357
self.connections.remove(connection)
357358
logger.info("Stopping interaction %r %r", *addr)
359+
except EOFError:
360+
# Happens either when the connection is closed by the client
361+
# (e.g., when the user types 'control-]', then 'quit' in the
362+
# telnet client) or when the user types control-d in a prompt
363+
# and this is not handled by the interact function.
364+
logger.info("Unhandled EOFError in telnet application.")
365+
except KeyboardInterrupt:
366+
# Unhandled control-c propagated by a prompt.
367+
logger.info("Unhandled KeyboardInterrupt in telnet application.")
358368
except BaseException as e:
359369
print("Got %s" % type(e).__name__, e)
360370
import traceback

0 commit comments

Comments
 (0)