Example #1
0
def run(main):
    factory = MPDFactory()
    factory.connectionMade = main
    reactor.connectTCP(os.getenv("MPD_HOST", "localhost"), os.getenv("MPD_PORT", 6600), factory)
    reactor.run()
Example #2
0
# and the factory reconnected.
def connectionMade(protocol):
    # Called after the protocol processed the data without an error.
    # For a more "synchronous" way of doing this check doc/example3.py
    def mpdStatus(result):
        print "The server's status: %s" % result
        # We drop the current connection and you'll see, that the factory
        # takes care of the reconnection.
        protocol.transport.loseConnection()

    # Register a callback to get the actual result. You may consider to add
    # an errback as well to handle failures.
    protocol.status().addCallback(mpdStatus)


# When a connection dies this function gets called
def connectionLost(protocol, reason):
    print "Connection lost: %s" % reason


factory = MPDFactory()

# Register our callbacks. No Deferred like in example1, just plain
# callables.
factory.connectionMade = connectionMade
factory.connectionLost = connectionLost

# Connect our factory and run.
reactor.connectTCP(MPD_HOST, MPD_PORT, factory)
reactor.run()