Debian Codegraph @

examples/ssh.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/python
2 """
3 A demonstration of Conch, allowing you to SSH into a running Python server and
4 inspect objects at a Python prompt.
5
6 If you're using the system install of Twisted, you may need to install Conch
7 separately, e.g. on Ubuntu:
8
9 $ sudo apt-get install python-twisted-conch
10
11 Once you've started the program, you can ssh in by doing:
12
13 $ ssh admin@localhost -p 5022
14
15 The password is 'secret'. Once you've reached the Python prompt, you have
16 access to the app object, and can import code, etc.:
17
18 >>> 3 + 4
19 7
20 >>> print(app)
21 <flask.app.Flask object at 0x18e1690>
22
23 """
24
25 import logging
26
27 from flask import Flask
28 from crochet import setup, run_in_reactor
29 setup()
30
31 # Web server:
32 app = Flask(__name__)
33
34 @app.route('/')
35 def index():
36 return "Welcome to my boring web server!"
37
38
39 @run_in_reactor
40 def start_ssh_server(port, username, password, namespace):
41 """
42 Start an SSH server on the given port, exposing a Python prompt with the
43 given namespace.
44 """
45 # This is a lot of boilerplate, see http://tm.tl/6429 for a ticket to
46 # provide a utility function that simplifies this.
47 from twisted.internet import reactor
48 from twisted.conch.insults import insults
49 from twisted.conch import manhole, manhole_ssh
50 from twisted.cred.checkers import (
51 InMemoryUsernamePasswordDatabaseDontUse as MemoryDB)
52 from twisted.cred.portal import Portal
53
54 sshRealm = manhole_ssh.TerminalRealm()
55 def chainedProtocolFactory():
56 return insults.ServerProtocol(manhole.Manhole, namespace)
57 sshRealm.chainedProtocolFactory = chainedProtocolFactory
58
59 sshPortal = Portal(sshRealm, [MemoryDB(**{username: password})])
60 reactor.listenTCP(port, manhole_ssh.ConchFactory(sshPortal),
61 interface="127.0.0.1")
62
63
64 if __name__ == '__main__':
65 import sys
66 logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
67 start_ssh_server(5022, "admin", "secret", {"app": app})
68 app.run()