File: telnet_echo.tac

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (48 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Simple echo server that echoes back client input.

You can run this .tac file directly with:
    twistd -ny telnet_echo.tac

This demo sets up a listening port on 6023 which accepts telnet connections.
No login for the telnet server is required.
"""

from twisted.application.internet import TCPServer
from twisted.application.service import Application
from twisted.conch.telnet import TelnetProtocol, TelnetTransport
from twisted.internet.protocol import ServerFactory


class TelnetEcho(TelnetProtocol):
    def enableRemote(self, option):
        self.transport.write(b"You tried to enable " + option + b" (I rejected it)\r\n")
        return False

    def disableRemote(self, option):
        self.transport.write(b"You disabled " + option + b"\r\n")

    def enableLocal(self, option):
        self.transport.write(
            b"You tried to make me enable "
            + option
            + b" (I rejected it)\r\n" % (option,)
        )
        return False

    def disableLocal(self, option):
        self.transport.write(b"You asked me to disable " + option + "\r\n")

    def dataReceived(self, data):
        self.transport.write(b"I received " + data + b" from you\r\n")


factory = ServerFactory()
factory.protocol = lambda: TelnetTransport(TelnetEcho)
service = TCPServer(6023, factory)

application = Application("Telnet Echo Server")
service.setServiceParent(application)