~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/conch/examples/demo_recvline.tac

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
# You can run this .tac file directly with:
 
5
#    twistd -ny demo_recvline.tac
 
6
 
 
7
"""Demonstrates line-at-a-time handling with basic line-editing support.
 
8
 
 
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.
 
14
 
 
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.
 
19
"""
 
20
 
 
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
 
25
 
 
26
from twisted.internet import protocol
 
27
from twisted.application import internet, service
 
28
from twisted.cred import checkers, portal
 
29
 
 
30
class DemoRecvLine(recvline.HistoricRecvLine):
 
31
    """Simple echo protocol.
 
32
 
 
33
    Accepts lines of input and writes them back to its connection.  If
 
34
    a line consisting solely of \"quit\" is received, the connection
 
35
    is dropped.
 
36
    """
 
37
 
 
38
    def lineReceived(self, line):
 
39
        if line == "quit":
 
40
            self.terminal.loseConnection()
 
41
        self.terminal.write(line)
 
42
        self.terminal.nextLine()
 
43
        self.terminal.write(self.ps[self.pn])
 
44
 
 
45
def makeService(args):
 
46
    checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(username="password")
 
47
 
 
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)
 
55
 
 
56
    def chainProtocolFactory():
 
57
        return insults.ServerProtocol(
 
58
            args['protocolFactory'],
 
59
            *args.get('protocolArgs', ()),
 
60
            **args.get('protocolKwArgs', {}))
 
61
 
 
62
    rlm = TerminalRealm()
 
63
    rlm.chainedProtocolFactory = chainProtocolFactory
 
64
    ptl = portal.Portal(rlm, [checker])
 
65
    f = ConchFactory(ptl)
 
66
    csvc = internet.TCPServer(args['ssh'], f)
 
67
 
 
68
    m = service.MultiService()
 
69
    tsvc.setServiceParent(m)
 
70
    csvc.setServiceParent(m)
 
71
    return m
 
72
 
 
73
application = service.Application("Insults RecvLine Demo")
 
74
 
 
75
makeService({'protocolFactory': DemoRecvLine,
 
76
             'telnet': 6023,
 
77
             'ssh': 6022}).setServiceParent(application)