1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
4
# You can run this .tac file directly with:
5
# twistd -ny demo_recvline.tac
7
"""Demonstrates line-at-a-time handling with basic line-editing support.
9
This is a variation on the echo server. It sets up two listening
10
ports: one on 6022 which accepts ssh connections; one on 6023 which
11
accepts telnet connections. No login for the telnet server is
12
required; for the ssh server, \"username\" is the username and
13
\"password\" is the password.
15
The demo protocol defined in this module is handed a line of input at
16
a time, which it simply writes back to the connection.
17
HistoricRecvline, which the demo protocol subclasses, provides basic
18
line editing and input history features.
21
from twisted.conch import recvline
22
from twisted.conch.insults import insults
23
from twisted.conch.telnet import TelnetTransport, TelnetBootstrapProtocol
24
from twisted.conch.manhole_ssh import ConchFactory, TerminalRealm
26
from twisted.internet import protocol
27
from twisted.application import internet, service
28
from twisted.cred import checkers, portal
30
class DemoRecvLine(recvline.HistoricRecvLine):
31
"""Simple echo protocol.
33
Accepts lines of input and writes them back to its connection. If
34
a line consisting solely of \"quit\" is received, the connection
38
def lineReceived(self, line):
40
self.terminal.loseConnection()
41
self.terminal.write(line)
42
self.terminal.nextLine()
43
self.terminal.write(self.ps[self.pn])
45
def makeService(args):
46
checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(username="password")
48
f = protocol.ServerFactory()
49
f.protocol = lambda: TelnetTransport(TelnetBootstrapProtocol,
50
insults.ServerProtocol,
51
args['protocolFactory'],
52
*args.get('protocolArgs', ()),
53
**args.get('protocolKwArgs', {}))
54
tsvc = internet.TCPServer(args['telnet'], f)
56
def chainProtocolFactory():
57
return insults.ServerProtocol(
58
args['protocolFactory'],
59
*args.get('protocolArgs', ()),
60
**args.get('protocolKwArgs', {}))
63
rlm.chainedProtocolFactory = chainProtocolFactory
64
ptl = portal.Portal(rlm, [checker])
66
csvc = internet.TCPServer(args['ssh'], f)
68
m = service.MultiService()
69
tsvc.setServiceParent(m)
70
csvc.setServiceParent(m)
73
application = service.Application("Insults RecvLine Demo")
75
makeService({'protocolFactory': DemoRecvLine,
77
'ssh': 6022}).setServiceParent(application)