Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import select
- # Telnet Server
- # By MegaLoler
- # Version 0.1
- # The TelnetClientConnection class
- # This class handles the client connections
- # Subclass this to define your own event handlers and then pass your subclass as the ConnectionClass argument in the TelnetServer class
- class TelnetClientConnection:
- def __init__(self, sock, addr):
- self.sock = sock
- self.addr = addr
- self.buf = ""
- self.connected = True
- self.onConnect()
- def recv(self, data):
- try:
- data = data.decode()
- self.buf += data
- for char in data:
- self.onCharInput(char)
- if "\r\n" in self.buf:
- lines = self.buf.split("\r\n")
- elif "\r\x00" in self.buf:
- lines = self.buf.split("\r\x00")
- else:
- lines = None
- if lines:
- self.buf = lines.pop()
- for line in lines:
- self.onLineInput(line.strip())
- except UnicodeDecodeError as exception:
- pass
- # Commands
- # Disconnects the client
- def disconnect(self):
- self.sock.close()
- self.connected = False
- self.onDisconnect()
- # Sends exact data
- def send(self, data):
- if self.connected: self.sock.send(data.encode())
- # Sends a line of data
- def sendLine(self, line):
- self.send(line.strip() + "\r\n")
- # Events
- # Triggered when the connection is established
- def onConnect(self):
- print("[TELNET] " + str(self.addr) + " has connected.")
- self.sendLine("Welcome to this telnet server thingy!")
- # Triggered when the connection is broken
- def onDisconnect(self):
- print("[TELNET] " + str(self.addr) + " has disconnected.")
- # Triggered when a single character is sent from the client (only works if the telnet client is in char mode)
- def onCharInput(self, char):
- pass
- # Triggered when the client presses enter
- def onLineInput(self, line):
- print("[TELNET] " + str(self.addr) + ": " + line)
- self.sendLine("I'd like to inform you that you typed \"" + line + "\"")
- # The TelnetServer class
- # Use the ConnectionClass parameter when you define your own subclasses of TelnetClientConnection to handle the events
- class TelnetServer:
- def __init__(self, host = socket.gethostname(), port = 23, maxQueue = 5, ConnectionClass = TelnetClientConnection):
- self.sock = socket.socket()
- self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- self.sock.bind((host, port))
- self.sock.listen(maxQueue)
- self.ConnectionClass = ConnectionClass
- self.clients = {}
- self.running = False
- # Start the server
- def start(self):
- if not self.running:
- self.running = True
- while self.running:
- (rlist, wlist, xlist) = select.select(list(self.clients.keys()) + [self.sock], [], [])
- for s in rlist:
- if s == self.sock:
- (client, address) = self.sock.accept()
- self.clients[client] = self.ConnectionClass(client, address)
- else:
- data = s.recv(1024)
- if len(data) == 0:
- self.clients[client].disconnect()
- del self.clients[client]
- else:
- self.clients[client].recv(data)
- # Stop the server
- def stop(self):
- if self.running:
- self.running = False
- if __name__ == "__main__":
- TelnetServer(port = 1234).start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement