Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fincham/3d0b7de0505d9f98d046ec411a396d5f to your computer and use it in GitHub Desktop.
Save fincham/3d0b7de0505d9f98d046ec411a396d5f to your computer and use it in GitHub Desktop.
def option_negotiator(socket, command, option):
# telnet really sucks, if you wanted more proof:
if command in (DO, DONT):
if command == DO and option == TTYPE: # "can you tell me your terminal type?"
socket.sendall(IAC + WILL + TTYPE) # "of course I will! let's do a whole pointless subnegotiation!"
else: # for all other commands just refuse
socket.sendall(IAC + WONT + option)
elif command in (WILL, WONT):
if command == WILL and option == ECHO: # "I will echo all characters back to you"
socket.sendall(IAC + DO + ECHO) # "yes please, echo all characters"
else: # all other offers just reply with a "don't"
socket.sendall(IAC + DONT + option)
elif command == SE: # a subnegotiation has started (hopefully for terminal-type)
if self.sock.sbdataq == TTYPE + chr(1): # reply with terminal-type, chr(1) = "SEND", chr(0) = "IS"
socket.sendall(IAC + SB + TTYPE + chr(0) + 'vt220' + IAC + SE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment