Python twisted.internet.reactor.listenUDP() Examples

The following are 30 code examples of twisted.internet.reactor.listenUDP(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.reactor , or try the search function .
Example #1
Source File: test_udp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
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 
Example #2
Source File: test_udp.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def testRebind(self):
        # Ensure binding the same DatagramProtocol repeatedly invokes all
        # the right callbacks.
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")

        def cbStarted(ignored, port):
            return port.stopListening()

        def cbStopped(ignored):
            d = server.startedDeferred = defer.Deferred()
            p = reactor.listenUDP(0, server, interface="127.0.0.1")
            return d.addCallback(cbStarted, p)

        return d.addCallback(cbStarted, p) 
Example #3
Source File: test_udp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_rebind(self):
        """
        Re-listening with the same L{DatagramProtocol} re-invokes the
        C{startProtocol} callback.
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")

        def cbStarted(ignored, port):
            return port.stopListening()

        def cbStopped(ignored):
            d = server.startedDeferred = defer.Deferred()
            p = reactor.listenUDP(0, server, interface="127.0.0.1")
            return d.addCallback(cbStarted, p)

        return d.addCallback(cbStarted, p) 
Example #4
Source File: test_udp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_startStop(self):
        """
        The L{DatagramProtocol}'s C{startProtocol} and C{stopProtocol}
        methods are called when its transports starts and stops listening,
        respectively.
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        port1 = reactor.listenUDP(0, server, interface="127.0.0.1")
        def cbStarted(ignored):
            self.assertEqual(server.started, 1)
            self.assertEqual(server.stopped, 0)
            return port1.stopListening()
        def cbStopped(ignored):
            self.assertEqual(server.stopped, 1)
        return d.addCallback(cbStarted).addCallback(cbStopped) 
Example #5
Source File: test_udp.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_startStop(self):
        """
        The L{DatagramProtocol}'s C{startProtocol} and C{stopProtocol}
        methods are called when its transports starts and stops listening,
        respectively.
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        port1 = reactor.listenUDP(0, server, interface="127.0.0.1")
        def cbStarted(ignored):
            self.assertEqual(server.started, 1)
            self.assertEqual(server.stopped, 0)
            return port1.stopListening()
        def cbStopped(ignored):
            self.assertEqual(server.stopped, 1)
        return d.addCallback(cbStarted).addCallback(cbStopped) 
Example #6
Source File: test_udp.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_rebind(self):
        """
        Re-listening with the same L{DatagramProtocol} re-invokes the
        C{startProtocol} callback.
        """
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")

        def cbStarted(ignored, port):
            return port.stopListening()

        def cbStopped(ignored):
            d = server.startedDeferred = defer.Deferred()
            p = reactor.listenUDP(0, server, interface="127.0.0.1")
            return d.addCallback(cbStarted, p)

        return d.addCallback(cbStarted, p) 
Example #7
Source File: test_udp.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testBindError(self):
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        port = reactor.listenUDP(0, server, interface='127.0.0.1')

        def cbStarted(ignored):
            self.assertEquals(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 
Example #8
Source File: test_udp.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_badConnect(self):
        """
        A call to the transport's connect method fails with an
        L{InvalidAddressError} when a non-IP address is passed as the host
        value.

        A call to a transport's connect method fails with a L{RuntimeError}
        when the transport is already connected.
        """
        client = GoodClient()
        port = reactor.listenUDP(0, client, interface="127.0.0.1")
        self.assertRaises(error.InvalidAddressError, client.transport.connect,
                          "localhost", 80)
        client.transport.connect("127.0.0.1", 80)
        self.assertRaises(RuntimeError, client.transport.connect,
                          "127.0.0.1", 80)
        return port.stopListening() 
Example #9
Source File: test_udp.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testRebind(self):
        # Ensure binding the same DatagramProtocol repeatedly invokes all
        # the right callbacks.
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")

        def cbStarted(ignored, port):
            return port.stopListening()

        def cbStopped(ignored):
            d = server.startedDeferred = defer.Deferred()
            p = reactor.listenUDP(0, server, interface="127.0.0.1")
            return d.addCallback(cbStarted, p)

        return d.addCallback(cbStarted, p) 
Example #10
Source File: channel.py    From marionette with Apache License 2.0 6 votes vote down vote up
def start_connection(transport_protocol, port, callback):
    if transport_protocol == 'tcp':
        factory = MyClientFactory(callback)
        factory.protocol = MyClient
        reactor.connectTCP(marionette_tg.conf.get("server.server_ip"),
                int(port), factory)
    else: #udp
        reactor.listenUDP(0, MyClient(callback, 'udp', marionette_tg.conf.get("server.server_ip"),
            int(port)), maxPacketSize=65535)
        #reactor.listenUDP(0, MyUdpClient(marionette_tg.conf.get("server.server_ip"),
        #    int(port), callback), maxPacketSize=65535)


    return True


#### Server async. classes 
Example #11
Source File: channel.py    From marionette with Apache License 2.0 6 votes vote down vote up
def start_listener(transport_protocol, port):
    retval = port

    if not port or not listening_sockets_.get(port):
        try:
            if transport_protocol == 'tcp':
                factory = protocol.Factory()
                factory.protocol = MyServer
                connector = reactor.listenTCP(int(port), factory,
                        interface=marionette_tg.conf.get("server.server_ip"))
            else: #udp
                connector = reactor.listenUDP(int(port), MyServer('udp'),
                        interface=marionette_tg.conf.get("server.server_ip"), maxPacketSize=65535)
            port = connector.getHost().port
            listening_sockets_[port] = connector
            retval = port
        except twisted.internet.error.CannotListenError as e:
            retval = False

    return retval 
Example #12
Source File: test_names.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def setUp(self):
        self.factory = server.DNSServerFactory([
            test_domain_com, reverse_domain, my_domain_com
        ], verbose=2)

        p = dns.DNSDatagramProtocol(self.factory)

        while 1:
            self.listenerTCP = reactor.listenTCP(0, self.factory, interface="127.0.0.1")
            port = self.listenerTCP.getHost().port

            try:
                self.listenerUDP = reactor.listenUDP(port, p, interface="127.0.0.1")
            except error.CannotListenError:
                self.listenerTCP.stopListening()
            else:
                break

        self.resolver = client.Resolver(servers=[('127.0.0.1', port)]) 
Example #13
Source File: latency_test.py    From wifibroadcast with GNU General Public License v3.0 6 votes vote down vote up
def run_test(port_in, port_out, size, count, rate):
    p1 = PacketSource(('127.0.0.1', port_in), size, count, rate)
    p2 = PacketSink()

    ep1 = reactor.listenUDP(0, p1)
    ep2 = reactor.listenUDP(port_out, p2)

    yield p1.df
    yield p2.df
    yield p1.start()
    yield df_sleep(1)

    sent = count
    lost = count - p2.count
    bitrate = rate * size * 8 / 1e6

    log.msg('Sent %d, Lost %d, Packet rate: %d pkt/s, Bitrate: %.2f MBit/s, Lmin %f, Lmax %f, Lavg %f' % \
            (sent, lost, rate, bitrate, p2.lmin, p2.lmax, p2.lavg / p2.count if p2.count else -1))

    yield ep1.stopListening()
    yield ep2.stopListening()

    defer.returnValue((lost, p2.lavg / p2.count if p2.count else -1, bitrate)) 
Example #14
Source File: test_udp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_badConnect(self):
        """
        A call to the transport's connect method fails with an
        L{InvalidAddressError} when a non-IP address is passed as the host
        value.

        A call to a transport's connect method fails with a L{RuntimeError}
        when the transport is already connected.
        """
        client = GoodClient()
        port = reactor.listenUDP(0, client, interface="127.0.0.1")
        self.assertRaises(error.InvalidAddressError, client.transport.connect,
                          "localhost", 80)
        client.transport.connect("127.0.0.1", 80)
        self.assertRaises(RuntimeError, client.transport.connect,
                          "127.0.0.1", 80)
        return port.stopListening() 
Example #15
Source File: test_udp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testStartStop(self):
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        port1 = reactor.listenUDP(0, server, interface="127.0.0.1")
        def cbStarted(ignored):
            self.assertEquals(server.started, 1)
            self.assertEquals(server.stopped, 0)
            return port1.stopListening()
        def cbStopped(ignored):
            self.assertEquals(server.stopped, 1)
        return d.addCallback(cbStarted).addCallback(cbStopped) 
Example #16
Source File: test_udp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testOldAddress(self):
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        p = reactor.listenUDP(0, server, interface="127.0.0.1")
        def cbStarted(ignored):
            addr = p.getHost()
            self.assertEquals(addr, ('INET_UDP', addr.host, addr.port))
            return p.stopListening()
        return d.addCallback(cbStarted) 
Example #17
Source File: test_append.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def start(self, port=0, boot_strap=[]):
        self.kserver = self.server_type()
        self.kserver.bootstrap(boot_strap)

        self.port = threads.blockingCallFromThread(reactor, reactor.listenUDP, port, self.kserver.protocol)
        print "Starting server:", self.port

        time.sleep(.2)

        return self.port.getHost().port, self.kserver 
Example #18
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def start(self, port=0, iface='', bootstrap=None):
        if bootstrap is None:
            bootstrap = []
        self.kserver = self.server_type()
        self.kserver.bootstrap(bootstrap)

        self.port = reactor.listenUDP(port, self.kserver.protocol, interface=iface)

        return self.port.getHost().host, self.port.getHost().port 
Example #19
Source File: dht_server.py    From calvin-base with Apache License 2.0 5 votes vote down vote up
def start(self, port=0, iface='', bootstrap=None):
        if bootstrap is None:
            bootstrap = []

        self.kserver = self.server_type(id=self.id, node_name=self.node_name, runtime_credentials=self.runtime_credentials)
        self.kserver.bootstrap(bootstrap)

        self.port = reactor.listenUDP(port,
                                        self.kserver.protocol,
                                        interface=iface)

        return self.port.getHost().host, self.port.getHost().port 
Example #20
Source File: test_sip.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.proxy = sip.RegisterProxy(host="127.0.0.1")
        self.registry = sip.InMemoryRegistry("bell.example.com")
        self.proxy.registry = self.proxy.locator = self.registry
        self.serverPort = reactor.listenUDP(0, self.proxy, interface="127.0.0.1")
        self.client = Client()
        self.clientPort = reactor.listenUDP(0, self.client, interface="127.0.0.1")
        self.serverAddress = (self.serverPort.getHost().host,
                              self.serverPort.getHost().port) 
Example #21
Source File: dns.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def startListening(self):
        from twisted.internet import reactor
        reactor.listenUDP(0, self, maxPacketSize=512) 
Example #22
Source File: test_mail.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def setUpDNS(self):
    self.auth = TestAuthority()
    factory = server.DNSServerFactory([self.auth])
    protocol = dns.DNSDatagramProtocol(factory)
    while 1:
        self.port = reactor.listenTCP(0, factory, interface='127.0.0.1')
        portNumber = self.port.getHost().port

        try:
            self.udpPort = reactor.listenUDP(portNumber, protocol, interface='127.0.0.1')
        except CannotListenError:
            self.port.stopListening()
        else:
            break
    self.resolver = client.Resolver(servers=[('127.0.0.1', portNumber)]) 
Example #23
Source File: app.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def listenUDP(self, port, proto, interface='', maxPacketSize=8192):
        """
        Connects a given DatagramProtocol to the given numeric UDP port.
        """
        self.udpPorts.append((port, proto, interface, maxPacketSize))
        if self.running:
            from twisted.internet import reactor
            return reactor.listenUDP(port, proto, interface, maxPacketSize) 
Example #24
Source File: proxyfuzz.py    From darkc0de-old-stuff with GNU General Public License v3.0 5 votes vote down vote up
def startudpproxy():
	reactor.listenUDP(localport, Server())
	reactor.run() 
Example #25
Source File: test_internet.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testUDP(self):
            p = reactor.listenUDP(0, protocol.DatagramProtocol())
            portNo = p.getHost().port
            self.assertNotEqual(str(p).find(str(portNo)), -1,
                                "%d not found in %s" % (portNo, p))
            return p.stopListening() 
Example #26
Source File: test_udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        """Start a UDP port"""
        self.server = Server()
        self.port = reactor.listenUDP(0, self.server, interface='127.0.0.1') 
Example #27
Source File: test_udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testPortRepr(self):
        client = GoodClient()
        p = reactor.listenUDP(0, client)
        portNo = str(p.getHost().port)
        self.failIf(repr(p).find(portNo) == -1)
        def stoppedListening(ign):
            self.failIf(repr(p).find(portNo) != -1)
        d = defer.maybeDeferred(p.stopListening)
        d.addCallback(stoppedListening)
        return d 
Example #28
Source File: test_udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testBadConnect(self):
        client = GoodClient()
        port = reactor.listenUDP(0, client, interface="127.0.0.1")
        self.assertRaises(ValueError, client.transport.connect,
                          "localhost", 80)
        client.transport.connect("127.0.0.1", 80)
        self.assertRaises(RuntimeError, client.transport.connect,
                          "127.0.0.1", 80)
        return port.stopListening() 
Example #29
Source File: test_udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testConnectionRefused(self):
        # assume no one listening on port 80 UDP
        client = GoodClient()
        clientStarted = client.startedDeferred = defer.Deferred()
        port = reactor.listenUDP(0, client, interface="127.0.0.1")

        server = Server()
        serverStarted = server.startedDeferred = defer.Deferred()
        port2 = reactor.listenUDP(0, server, interface="127.0.0.1")

        d = defer.DeferredList(
            [clientStarted, serverStarted],
            fireOnOneErrback=True)

        def cbStarted(ignored):
            connectionRefused = client.startedDeferred = defer.Deferred()
            client.transport.connect("127.0.0.1", 80)

            for i in range(10):
                client.transport.write(str(i))
                server.transport.write(str(i), ("127.0.0.1", 80))

            return self.assertFailure(
                connectionRefused,
                error.ConnectionRefusedError)

        d.addCallback(cbStarted)

        def cbFinished(ignored):
            return defer.DeferredList([
                defer.maybeDeferred(port.stopListening),
                defer.maybeDeferred(port2.stopListening)],
                fireOnOneErrback=True)

        d.addCallback(cbFinished)
        return d 
Example #30
Source File: test_udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testStartStop(self):
        server = Server()
        d = server.startedDeferred = defer.Deferred()
        port1 = reactor.listenUDP(0, server, interface="127.0.0.1")
        def cbStarted(ignored):
            self.assertEquals(server.started, 1)
            self.assertEquals(server.stopped, 0)
            return port1.stopListening()
        def cbStopped(ignored):
            self.assertEquals(server.stopped, 1)
        return d.addCallback(cbStarted).addCallback(cbStopped)