1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
4 # You can run this .tac file directly with:
5 # twistd -ny demo_draw.tac
7 """A trivial drawing application.
9 Clients are allowed to connect and spew various characters out over
10 the terminal. Spacebar changes the drawing character, while the arrow
14 from twisted.conch.insults import insults
15 from twisted.conch.telnet import TelnetTransport, TelnetBootstrapProtocol
16 from twisted.conch.manhole_ssh import ConchFactory, TerminalRealm
18 from twisted.internet import protocol
19 from twisted.application import internet, service
20 from twisted.cred import checkers, portal
22 class Draw(insults.TerminalProtocol):
23 """Protocol which accepts arrow key and spacebar input and places
24 the requested characters onto the terminal.
26 cursors = list('!@#$%^&*()_+-=')
28 def connectionMade(self):
29 self.terminal.eraseDisplay()
30 self.terminal.resetModes([insults.modes.IRM])
31 self.cursor = self.cursors[0]
33 def keystrokeReceived(self, keyID, modifier):
34 if keyID == self.terminal.UP_ARROW:
35 self.terminal.cursorUp()
36 elif keyID == self.terminal.DOWN_ARROW:
37 self.terminal.cursorDown()
38 elif keyID == self.terminal.LEFT_ARROW:
39 self.terminal.cursorBackward()
40 elif keyID == self.terminal.RIGHT_ARROW:
41 self.terminal.cursorForward()
43 self.cursor = self.cursors[(self.cursors.index(self.cursor) + 1) % len(self.cursors)]
46 self.terminal.write(self.cursor)
47 self.terminal.cursorBackward()
49 def makeService(args):
50 checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(username="password")
52 f = protocol.ServerFactory()
53 f.protocol = lambda: TelnetTransport(TelnetBootstrapProtocol,
54 insults.ServerProtocol,
55 args['protocolFactory'],
56 *args.get('protocolArgs', ()),
57 **args.get('protocolKwArgs', {}))
58 tsvc = internet.TCPServer(args['telnet'], f)
60 def chainProtocolFactory():
61 return insults.ServerProtocol(
62 args['protocolFactory'],
63 *args.get('protocolArgs', ()),
64 **args.get('protocolKwArgs', {}))
67 rlm.chainedProtocolFactory = chainProtocolFactory
68 ptl = portal.Portal(rlm, [checker])
70 csvc = internet.TCPServer(args['ssh'], f)
72 m = service.MultiService()
73 tsvc.setServiceParent(m)
74 csvc.setServiceParent(m)
77 application = service.Application("Insults Demo App")
78 makeService({'protocolFactory': Draw,
80 'ssh': 6022}).setServiceParent(application)