'Get a TCP port from a reactor, wrapping an already-initialized file
descriptor.'

def getListeningPort(self, reactor, factory, port=0, interface=''):

    if IReactorSocket.providedBy(reactor):
        if (':' in interface):
            domain = socket.AF_INET6
            address = socket.getaddrinfo(interface, port)[0][4]
        else:
            domain = socket.AF_INET
            address = (interface, port)
        portSock = socket.socket(domain)
        portSock.bind(address)
        portSock.listen(3)
        portSock.setblocking(False)
        try:
            return reactor.adoptStreamPort(portSock.fileno(), portSock.family, factory)
        finally:
            portSock.fileno()
            portSock.close()
    else:
        raise SkipTest('Reactor    does    not    provide    IReactorSocket')
