當前位置: 首頁>>代碼示例>>Python>>正文


Python error.CannotListenError方法代碼示例

本文整理匯總了Python中twisted.internet.error.CannotListenError方法的典型用法代碼示例。如果您正苦於以下問題:Python error.CannotListenError方法的具體用法?Python error.CannotListenError怎麽用?Python error.CannotListenError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在twisted.internet.error的用法示例。


在下文中一共展示了error.CannotListenError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _bindSocket

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def _bindSocket(self):
        try:
            skt = self.createSocket()
            skt.bind((self.interface, self.port))
        except socket.error as le:
            raise error.CannotListenError(self.interface, self.port, le)

        # Make sure that if we listened on port 0, we update that to
        # reflect what the OS actually assigned us.
        self._realPortNumber = skt.getsockname()[1]

        log.msg("%s starting on %s" % (
                self._getLogPrefix(self.protocol), self._realPortNumber))

        self.connected = True
        self.socket = skt
        self.getFileHandle = self.socket.fileno 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:19,代碼來源:udp.py

示例2: test_endpointListenFailure

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_endpointListenFailure(self):
        """
        When an endpoint tries to listen on an already listening port, a
        C{CannotListenError} failure is errbacked.
        """
        factory = object()
        exception = error.CannotListenError('', 80, factory)
        mreactor = RaisingMemoryReactor(listenException=exception)

        ep, ignoredArgs, ignoredDest = self.createServerEndpoint(
            mreactor, factory)

        d = ep.listen(object())

        receivedExceptions = []

        def checkFailure(f):
            receivedExceptions.append(f.value)

        d.addErrback(checkFailure)

        self.assertEqual(receivedExceptions, [exception]) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:24,代碼來源:test_endpoints.py

示例3: test_disallowedPort

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_disallowedPort(self):
        """
        If a port number is initially selected which cannot be bound, the
        L{CannotListenError} is handled and another port number is attempted.
        """
        ports = []

        class FakeReactor(object):
            def listenUDP(self, port, *args, **kwargs):
                ports.append(port)
                if len(ports) == 1:
                    raise CannotListenError(None, port, None)

        resolver = client.Resolver(servers=[('example.com', 53)])
        resolver._reactor = FakeReactor()

        resolver._connectedProtocol()
        self.assertEqual(len(set(ports)), 2) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:20,代碼來源:test_client.py

示例4: test_cannotBind

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_cannotBind(self):
        """
        L{IReactorTCP.listenTCP} raises L{error.CannotListenError} if the
        address to listen on is already in use.
        """
        f = MyServerFactory()

        p1 = reactor.listenTCP(0, f, interface='127.0.0.1')
        self.addCleanup(p1.stopListening)
        n = p1.getHost().port
        dest = p1.getHost()
        self.assertEqual(dest.type, "TCP")
        self.assertEqual(dest.host, "127.0.0.1")
        self.assertEqual(dest.port, n)

        # make sure new listen raises error
        self.assertRaises(error.CannotListenError,
                          reactor.listenTCP, n, f, interface='127.0.0.1') 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:20,代碼來源:test_tcp.py

示例5: test_bindError

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_bindError(self):
        """
        A L{CannotListenError} exception is raised when attempting to bind a
        second protocol instance to an already bound port
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        port = reactor.listenUDP(0, server, interface='127.0.0.1')

        def cbStarted(ignored):
            self.assertEqual(port.getHost(), server.transport.getHost())
            server2 = Server()
            self.assertRaises(
                error.CannotListenError,
                reactor.listenUDP, port.getHost().port, server2,
                interface='127.0.0.1')
        d.addCallback(cbStarted)

        def cbFinished(ignored):
            return port.stopListening()
        d.addCallback(cbFinished)
        return d 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:24,代碼來源:test_udp.py

示例6: test_socketLocking

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_socketLocking(self):
        """
        L{IReactorUNIX.listenUNIX} raises L{error.CannotListenError} if passed
        the name of a file on which a server is already listening.
        """
        filename = self.mktemp()
        serverFactory = MyServerFactory()
        unixPort = reactor.listenUNIX(filename, serverFactory, wantPID=True)

        self.assertRaises(
            error.CannotListenError,
            reactor.listenUNIX, filename, serverFactory, wantPID=True)

        def stoppedListening(ign):
            unixPort = reactor.listenUNIX(filename, serverFactory, wantPID=True)
            return unixPort.stopListening()

        return unixPort.stopListening().addCallback(stoppedListening) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:20,代碼來源:test_unix.py

示例7: test_portRange

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_portRange(self):
        """
        L{FTP.passivePortRange} should determine the ports which
        L{FTP.getDTPPort} attempts to bind. If no port from that iterator can
        be bound, L{error.CannotListenError} should be raised, otherwise the
        first successful result from L{FTP.listenFactory} should be returned.
        """
        def listenFactory(portNumber, factory):
            if portNumber in (22032, 22033, 22034):
                raise error.CannotListenError('localhost', portNumber, 'error')
            return portNumber
        self.serverProtocol.listenFactory = listenFactory

        port = self.serverProtocol.getDTPPort(protocol.Factory())
        self.assertEqual(port, 0)

        self.serverProtocol.passivePortRange = xrange(22032, 65536)
        port = self.serverProtocol.getDTPPort(protocol.Factory())
        self.assertEqual(port, 22035)

        self.serverProtocol.passivePortRange = xrange(22032, 22035)
        self.assertRaises(error.CannotListenError,
                          self.serverProtocol.getDTPPort,
                          protocol.Factory()) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:26,代碼來源:test_ftp.py

示例8: _bindSocket

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def _bindSocket(self):
        """
        Open the tunnel.
        """
        log.msg(
            format="%(protocol)s starting on %(interface)s",
            protocol=self.protocol.__class__,
            interface=self.interface)
        try:
            fileno, interface = self._openTunnel(
                self.interface, self._mode | TunnelFlags.IFF_NO_PI)
        except (IOError, OSError) as e:
            raise error.CannotListenError(None, self.interface, e)

        self.interface = interface
        self._fileno = fileno

        self.connected = 1 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:20,代碼來源:tuntap.py

示例9: test_disallowedPortRepeatedly

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_disallowedPortRepeatedly(self):
        """
        If port numbers that cannot be bound are repeatedly selected,
        L{resolver._connectedProtocol} will give up eventually.
        """
        ports = []

        class FakeReactor(object):
            def listenUDP(self, port, *args, **kwargs):
                ports.append(port)
                raise CannotListenError(None, port, None)

        resolver = client.Resolver(servers=[('example.com', 53)])
        resolver._reactor = FakeReactor()

        self.assertRaises(CannotListenError, resolver._connectedProtocol)
        # 1000 is a good round number. I like it.
        self.assertEqual(len(ports), 1000) 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:20,代碼來源:test_client.py

示例10: test_runOutOfFiles

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_runOutOfFiles(self):
        """
        If the process is out of files, L{Resolver._connectedProtocol}
        will give up.
        """
        ports = []

        class FakeReactor(object):
            def listenUDP(self, port, *args, **kwargs):
                ports.append(port)
                err = OSError(errno.EMFILE, "Out of files :(")
                raise CannotListenError(None, port, err)

        resolver = client.Resolver(servers=[('example.com', 53)])
        resolver._reactor = FakeReactor()

        exc = self.assertRaises(CannotListenError, resolver._connectedProtocol)
        # The EMFILE-containing exception was raised, and it did not try
        # multiple times.
        self.assertEqual(exc.socketError.errno, errno.EMFILE)
        self.assertEqual(len(ports), 1) 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:23,代碼來源:test_client.py

示例11: test_portRange

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_portRange(self):
        """
        L{FTP.passivePortRange} should determine the ports which
        L{FTP.getDTPPort} attempts to bind. If no port from that iterator can
        be bound, L{error.CannotListenError} should be raised, otherwise the
        first successful result from L{FTP.listenFactory} should be returned.
        """
        def listenFactory(portNumber, factory):
            if portNumber in (22032, 22033, 22034):
                raise error.CannotListenError('localhost', portNumber, 'error')
            return portNumber
        self.serverProtocol.listenFactory = listenFactory

        port = self.serverProtocol.getDTPPort(protocol.Factory())
        self.assertEqual(port, 0)

        self.serverProtocol.passivePortRange = range(22032, 65536)
        port = self.serverProtocol.getDTPPort(protocol.Factory())
        self.assertEqual(port, 22035)

        self.serverProtocol.passivePortRange = range(22032, 22035)
        self.assertRaises(error.CannotListenError,
                          self.serverProtocol.getDTPPort,
                          protocol.Factory()) 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:26,代碼來源:test_ftp.py

示例12: test_endpointListenFailure

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_endpointListenFailure(self):
        """
        When an endpoint tries to listen on an already listening port, a
        C{CannotListenError} failure is errbacked.
        """
        factory = object()
        exception = error.CannotListenError('', 80, factory)
        mreactor = RaisingMemoryReactor(listenException=exception)

        ep, ignoredArgs, ignoredDest = self.createServerEndpoint(
            mreactor, factory)

        d = ep.listen(object())

        receivedExceptions = []

        def checkFailure(f):
            receivedExceptions.append(f.value)

        d.addErrback(checkFailure)

        self.assertEquals(receivedExceptions, [exception]) 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:24,代碼來源:test_endpoints.py

示例13: _connectedProtocol

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def _connectedProtocol(self):
        """
        Return a new L{DNSDatagramProtocol} bound to a randomly selected port
        number.
        """
        if 'protocol' in self.__dict__:
            # Some code previously asked for or set the deprecated `protocol`
            # attribute, so it probably expects that object to be used for
            # queries.  Give it back and skip the super awesome source port
            # randomization logic.  This is actually a really good reason to
            # remove this deprecated backward compatibility as soon as
            # possible. -exarkun
            return self.protocol
        proto = dns.DNSDatagramProtocol(self)
        while True:
            try:
                self._reactor.listenUDP(dns.randomSource(), proto)
            except error.CannotListenError:
                pass
            else:
                return proto 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:23,代碼來源:client.py

示例14: test_disallowedPort

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_disallowedPort(self):
        """
        If a port number is initially selected which cannot be bound, the
        L{CannotListenError} is handled and another port number is attempted.
        """
        ports = []

        class FakeReactor(object):
            def listenUDP(self, port, *args):
                ports.append(port)
                if len(ports) == 1:
                    raise error.CannotListenError(None, port, None)

        resolver = client.Resolver(servers=[('example.com', 53)])
        resolver._reactor = FakeReactor()

        proto = resolver._connectedProtocol()
        self.assertEqual(len(set(ports)), 2) 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:20,代碼來源:test_client.py

示例15: test_cannotBind

# 需要導入模塊: from twisted.internet import error [as 別名]
# 或者: from twisted.internet.error import CannotListenError [as 別名]
def test_cannotBind(self):
        """
        L{IReactorTCP.listenTCP} raises L{error.CannotListenError} if the
        address to listen on is already in use.
        """
        f = MyServerFactory()

        p1 = reactor.listenTCP(0, f, interface='127.0.0.1')
        self.addCleanup(p1.stopListening)
        n = p1.getHost().port
        dest = p1.getHost()
        self.assertEquals(dest.type, "TCP")
        self.assertEquals(dest.host, "127.0.0.1")
        self.assertEquals(dest.port, n)

        # make sure new listen raises error
        self.assertRaises(error.CannotListenError,
                          reactor.listenTCP, n, f, interface='127.0.0.1') 
開發者ID:kuri65536,項目名稱:python-for-android,代碼行數:20,代碼來源:test_tcp.py


注:本文中的twisted.internet.error.CannotListenError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。