Skip to content

Instantly share code, notes, and snippets.

@nhasbun
Forked from pedrominicz/telnet.py
Created April 27, 2022 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhasbun/396864b5654add4e8ecb290c9bb259ea to your computer and use it in GitHub Desktop.
Save nhasbun/396864b5654add4e8ecb290c9bb259ea to your computer and use it in GitHub Desktop.
Extremely simple Telnet server in Python.
#!/usr/bin/env python3
import socket
# Connect to the server with `telnet $HOSTNAME 5000`.
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(False)
server.bind((socket.gethostname(), 5000))
server.listen(5)
connections = []
while True:
try:
connection, address = server.accept()
connection.setblocking(False)
connections.append(connection)
except BlockingIOError:
pass
for connection in connections:
try:
message = connection.recv(4096)
except BlockingIOError:
continue
for connection in connections:
connection.send(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment