Skip to content

Commit

Permalink
Removes print and corrects Telnet symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ForceFledgling committed Nov 26, 2023
1 parent 1bfc4a3 commit 9b369df
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions asyncio_telnet/asyncio_telnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
TELNET_PORT = 23

# Telnet protocol characters (don't change)
IAC = bytes([255]) # "Interpret As Command"
DONT = bytes([254])
DO = bytes([253])
WONT = bytes([252])
WILL = bytes([251])
theNULL = bytes([0])
ECHO = bytes([1])
theNULL = bytes([0]) # Null (No operation)
ECHO = bytes([1]) # Echo
SB = bytes([250]) # Subnegotiation Begin
WILL = bytes([251]) # Will
WONT = bytes([252]) # Will Not
DO = bytes([253]) # Do
DONT = bytes([254]) # Do Not
IAC = bytes([255]) # Interpret As Command
SE = bytes([240]) # Subnegotiation End


class AsyncTelnet:

def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
self.debuglevel = 0
self.timeout = timeout
Expand All @@ -41,11 +44,10 @@ async def open(self, host, port=0):
raise

except Exception as e:
print(f"Error connecting to {host}:{port}: {e}")
raise
raise f"Error connecting to {host}:{port}: {e}"

async def write(self, buffer):
buffer = buffer.replace(b'\xff', b'\xff\xff')
buffer = buffer.replace(IAC, IAC + IAC)
self.writer.write(buffer)
await self.writer.drain()

Expand All @@ -72,7 +74,6 @@ async def read_until_eof(self, fast_mode=True):
response += data
except asyncio.TimeoutError:
break
print('response', response)
return response

async def __aenter__(self):
Expand All @@ -89,7 +90,6 @@ async def __aexit__(self, exc_type, exc_value, traceback):
await self.writer.drain()
self.writer.close()
await self.writer.wait_closed()
print("Connection closed")

async def filter_telnet_data(self, data):
"""
Expand All @@ -105,9 +105,9 @@ async def filter_telnet_data(self, data):
if command[1:2] in (WILL, WONT, DO, DONT, IAC):
# It's a command WONT, WILL, DO, DONT, or IAC, remove it
continue
elif command[1:2] == b'\xfa':
elif command[1:2] == SB:
# It's the beginning of option support, skip everything until IAC SE
iac_se_pos = data.find(b'\xff\xf0', i)
iac_se_pos = data.find(IAC + SE, i)
if iac_se_pos != -1:
i = iac_se_pos + 2
else:
Expand Down

0 comments on commit 9b369df

Please sign in to comment.