示例#1
0
def query(ctx):

    """"
    Execute subcommand through UNIX domain socket client.
    """

    if not ctx.opts.argv:
        print("No command %s" % ctx.opts.argv[0], file=ctx.err)
        return 1

    address = FilePath(ctx.opts.flags.address)

    factory = Factory()
    factory.ctx = ctx
    ctx.rs = 0

    factory.protocol = QueryProtocol
    factory.quiet = True
    factory.cmd = ' '.join(ctx.opts.argv)
    # DEBUG:
    # print('Passthrough command to backend via socket: %r' % factory.cmd, file=sys.stderr)

    endpoint = UNIXClientEndpoint(reactor, address.path)
    connected = endpoint.connect(factory)

    def succeeded(client):
        return client.whenDisconnected
    def failed(reason):
        print("Could not connect:", reason.getErrorMessage(), file=ctx.err)
    def disconnected(ignored):
        reactor.stop()

    connected.addCallbacks(succeeded, failed)
    connected.addCallback(disconnected)

    reactor.run()

    return factory.ctx.rs
示例#2
0
def serve(ctx, handlers, prerun=prerun, postrun=postrun):

    """
    Start protocol at socket address path. Handlers is a dict
    of sub-command names, and corresponding functions.
    See above for the two callbacks prerun and postrun.
    """

    address = FilePath(ctx.opts.flags.address)

    if address.exists():
        raise SystemExit("Cannot listen on an existing path")

    #startLogging(sys.stdout)

    serverFactory = Factory()
    serverFactory.ctx = ctx
    serverFactory.handlers = handlers
    serverFactory.prerun = prerun
    serverFactory.postrun = postrun
    serverFactory.protocol = ServerProtocol

    port = reactor.listenUNIX(address.path, serverFactory)
    reactor.run()