File: self_contained.py

package info (click to toggle)
python-mitogen 0.3.26-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,456 kB
  • sloc: python: 22,134; sh: 183; makefile: 74; perl: 19; ansic: 18
file content (52 lines) | stat: -rw-r--r-- 1,246 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
49
50
51
52
import mitogen
import mitogen.service


class FileService(mitogen.service.Service):
    """
    Simple file server, for demonstration purposes only! Use of this in
    real code would be a security vulnerability as it would permit children
    to read any file from the master's disk.
    """

    @mitogen.service.expose(policy=mitogen.service.AllowAny())
    @mitogen.service.arg_spec(spec={
        'path': str
    })
    def read_file(self, path):
        with open(path, 'rb') as fp:
            return fp.read()


def download_file(source_context, path):
    s = source_context.call_service(
        service_name=FileService,  # may also be string 'pkg.mod.FileService'
        method_name='read_file',
        path=path,
    )

    with open(path, 'w') as fp:
        fp.write(s)


def download_some_files(source_context, paths):
    for path in paths:
        download_file(source_context, path)


@mitogen.main()
def main(router):
    pool = mitogen.service.Pool(router, services=[
        FileService(router),
    ])

    remote = router.ssh(hostname='k3')
    remote.call(download_some_files,
        source_context=router.myself(),
        paths=[
            '/etc/passwd',
            '/etc/hosts',
        ]
    )
    pool.stop()