Skip to content

Commit

Permalink
Small fixes regarding previous commit about telnet changes.
Browse files Browse the repository at this point in the history
- Fix issue when closing the telnet connection.
- Handle `EOFError` and `KeyboardInterrupt` in telnet applications.
  • Loading branch information
jonathanslenders committed Mar 10, 2022
1 parent 97ac514 commit fe50f11
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion prompt_toolkit/contrib/telnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def isatty(self) -> bool:

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

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

0 comments on commit fe50f11

Please sign in to comment.