handel-server 1.55 KB
Newer Older
Vincent Michel's avatar
Vincent Michel committed
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3

"""Serve the handel interface over the network using zerorpc.

This requires python3, handel, zerorpc and msgpack_numpy.

Usage:

    $ ./handel-server 8888
Vincent Michel's avatar
Vincent Michel committed
10
    Serving handel on tcp://0.0.0.0:8888 ...
Vincent Michel's avatar
Vincent Michel committed
11
12
13
14
15
16
17
18

Test on the client machine using:

    $ zerorpc tcp://hostname:8888 -?
"""

# Imports

19
import argparse
Vincent Michel's avatar
Vincent Michel committed
20
21
22
23

import zerorpc
import msgpack_numpy

Vincent Michel's avatar
Vincent Michel committed
24
import handel.gevent
Vincent Michel's avatar
Vincent Michel committed
25
26
27
28
29
30
31
32
33
34
import handel.interface as hi

# Run server

def run(bind='0.0.0.0', port=8000):
    access = "tcp://{}:{}".format(bind, port)
    try:
        hi.init_handel()
        server = zerorpc.Server(hi)
        server.bind(access)
Vincent Michel's avatar
Vincent Michel committed
35
        print('Serving handel on {} ...'.format(access))
Vincent Michel's avatar
Vincent Michel committed
36
37
38
39
40
41
42
43
44
45
        try:
            server.run()
        except KeyboardInterrupt:
            print('Interrupted.')
        finally:
            server.close()
    finally:
        hi.exit()


46
47
48
49
50
51
52
# Parsing

def parse_args(args=None):
    parser = argparse.ArgumentParser(
        prog='handel-server',
        description='Serve the handel interface over the network using zerorpc')
    parser.add_argument(
Vincent Michel's avatar
Vincent Michel committed
53
        '--bind', '-b', default='0.0.0.0', metavar='address',
54
55
56
57
58
59
60
        help='Specify alternate bind address [default: all interfaces]')
    parser.add_argument(
        'port', action='store', default=8000, type=int, nargs='?',
        help='Specify alternate port [default: 8000]')
    return parser.parse_args(args)


Vincent Michel's avatar
Vincent Michel committed
61
# Main function
Vincent Michel's avatar
Vincent Michel committed
62
63

def main(args=None):
64
65
66
67
    namespace = parse_args(args)
    handel.gevent.patch()
    msgpack_numpy.patch()
    run(namespace.bind, namespace.port)
Vincent Michel's avatar
Vincent Michel committed
68
69
70
71


if __name__ == '__main__':
    main()