[PYTHON] Convenient for training newcomers? I wrote a Telnet practice server

I want newcomers to experience the operation of servers and NW devices with Telnet, but it is difficult to prepare a load balancer, etc., apart from just running Linux with a VM. That's why I wrote a practice server that simulates a Telnet connection using Python's socket server.

After inputting an appropriate command, write an if statement to output according to it, and connect with a terminal such as TeraTerm.

if command == "exit":
  sys.exit()

elif command == "ls":
  self.write("bin     etc     proc    usr     dev     home\r\n")

Since it speaks the Telnet protocol properly, it can be used in the terminal used in the field, and it can also be used as a laboratory table for macros.

server.py


import sys
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):

    prompt = "$ "

    def command(self,command):
        print(command)
        if command == "exit":
            sys.exit()

        elif command == "ls":
            self.write("bin     etc     proc    usr     dev     home\r\n")

        elif command == "ls -l":
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users 0 March 19 11:14 bin\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users 0 March 19 11:14 etc\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users 0 March 19 11:14 proc\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users 0 March 19 11:14 usr\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users 0 March 19 11:14 dev\r\n")
            self.write("drwxr-xr-x+ 1 t.uehara Domain Users 0 March 19 11:14 home\r\n")

    def write(self,str):
        self.request.sendall(bytes(str,"UTF-8"))

    def handle(self):

        buff = []

        while True:
            self.data = self.request.recv(1024)

            if self.data == bytes("\r\x00","UTF-8"): #new line
                self.write("\r\n")
                self.command("".join(buff))
                del buff[:]
                self.write(self.prompt)
            elif self.data == bytes("\x03","UTF-8"): # Ctrl+C
                sys.exit()
            else:
                self.request.sendall(self.data)
                try:
                    buff.append(self.data.decode("UTF-8"))
                except UnicodeDecodeError:
                    print(self.data)

if __name__ == "__main__":

    HOST, PORT = "localhost", 23

    server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

By the way, originally, I would like to interpret the command when connecting to Telnet, but for now I am returning the parrot. If you want to expand your own, the following site will be helpful.

http://www5e.biglobe.ne.jp/aji/3min/55.html

Command history and Tab completion are also impossible. This practice table is not suitable for those who say that our workplace is Zsh. But I wanted to secretly make the SSH version.

Recommended Posts

Convenient for training newcomers? I wrote a Telnet practice server
I wrote a demo program for linear transformation of a matrix
I made a client / server CLI tool for WebSocket (like Netcat for WebSocket)
AtCoder writer I wrote a script to aggregate the contests for each writer
I made a dash docset for Holoviews
I wrote unit tests for various languages
I wrote the code for Gibbs sampling
I made a library for actuarial science
I wrote a graph like R glmnet in Python for sparse modeling in Lasso