human
Complete this python program:
def listen(case, endpoint, factory):
assistant
#Here is the Python program you requested.

    """Listen on an endpoint and cleanup when the
    test case ends.
    :param case: The test case that will handle cleanup.
    :param IStreamServerEndpoint endpoint: The endpoint to listen on.
    :param IProtocolFactory factory: The factory for the server protocol.
    """
    d = endpoint.listen(factory)
    def registerCleanup(listeningPort):
        case.addCleanup(listeningPort.stopListening)
        return listeningPort
    d.addCallback(registerCleanup)
    return reportUnhandledErrors(case, d)