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()
def testAddingBadProtos_TooBig2(self): """Adding a protocol with a number >=2**16 raises an exception.""" e = rawudp.RawUDPProtocol() try: e.addProto(2**16+1, protocol.DatagramProtocol()) except TypeError, e: if e.args == ('Added protocol must fit in 16 bits',): pass else: raise
def testAddingBadProtos_TooSmall(self): """Adding a protocol with a negative number raises an exception.""" e = rawudp.RawUDPProtocol() try: e.addProto(-1, protocol.DatagramProtocol()) except TypeError, e: if e.args == ('Added protocol must be positive or zero',): pass else: raise
def testAddingBadProtos_TooBig(self): """Adding a protocol with a number >=2**16 raises an exception.""" e = rawudp.RawUDPProtocol() try: e.addProto(2**16, protocol.DatagramProtocol()) except TypeError as e: if e.args == ('Added protocol must fit in 16 bits',): pass else: raise else: raise AssertionError('addProto must raise an exception for bad protocols')
def testAddingBadProtos_TooSmall(self): """Adding a protocol with a negative number raises an exception.""" e = rawudp.RawUDPProtocol() try: e.addProto(-1, protocol.DatagramProtocol()) except TypeError as e: if e.args == ("Added protocol must be positive or zero", ): pass else: raise else: raise AssertionError( "addProto must raise an exception for bad protocols")
def testListenUnlistenUDP(self): a = app.Application("foo") f = protocol.DatagramProtocol() a.listenUDP(9999, f) a.listenUDP(9998, f) self.assertEquals(len(a.udpPorts), 2) a.unlistenUDP(9999) self.assertEquals(len(a.udpPorts), 1) a.listenUDP(9999, f, interface='127.0.0.1') self.assertEquals(len(a.udpPorts), 2) a.unlistenUDP(9999, '127.0.0.1') self.assertEquals(len(a.udpPorts), 1) a.unlistenUDP(9998) self.assertEquals(len(a.udpPorts), 0)
def testUDP(self): if not interfaces.IReactorUDP(reactor, None): raise unittest.SkipTest, "This reactor does not support UDP sockets" p = protocol.DatagramProtocol() t = internet.TCPServer(0, p) t.startService() num = t._port.getHost().port def onStop(ignored): t = internet.TCPServer(num, p) t.startService() return t.stopService() return defer.maybeDeferred(t.stopService).addCallback(onStop)
def testUDP(self): if not interfaces.IReactorUDP(reactor, None): raise unittest.SkipTest, "This reactor does not support UDP sockets" p = protocol.DatagramProtocol() t = internet.TCPServer(0, p) t.startService() num = t._port.getHost().port l = [] defer.maybeDeferred(t.stopService).addCallback(l.append) util.spinWhile(lambda: not l) t = internet.TCPServer(num, p) t.startService() l = [] defer.maybeDeferred(t.stopService).addCallback(l.append) util.spinWhile(lambda: not l)
def test_UDP(self): """ Test L{internet.UDPServer} with a random port: starting the service should give it valid port, and stopService should free it so that we can start a server on the same port again. """ if not interfaces.IReactorUDP(reactor, None): raise unittest.SkipTest("This reactor does not support UDP sockets") p = protocol.DatagramProtocol() t = internet.UDPServer(0, p) t.startService() num = t._port.getHost().port self.assertNotEquals(num, 0) def onStop(ignored): t = internet.UDPServer(num, p) t.startService() return t.stopService() return defer.maybeDeferred(t.stopService).addCallback(onStop)
def get_local_ip_for(target='A.ROOT-SERVERS.NET'): """Find out what our IP address is for use by a given target. @return: the IP address as a dotted-quad string which could be used by to connect to us. It might work for them, it might not. If there is no suitable address (perhaps we don't currently have an externally-visible interface), this will return None. """ try: target_ipaddr = socket.gethostbyname(target) except socket.gaierror: # DNS isn't running return None udpprot = protocol.DatagramProtocol() port = reactor.listenUDP(0, udpprot) try: udpprot.transport.connect(target_ipaddr, 7) localip = udpprot.transport.getHost().host except socket.error: # no route to that host localip = None port.stopListening() # note, this returns a Deferred return localip
def __init__(self, parsed_url): super(TwistedUDPTransport, self).__init__(parsed_url) self.protocol = protocol.DatagramProtocol() reactor.listenUDP(0, self.protocol)