本文整理汇总了Python中twisted.internet.reactor.listenUDP方法的典型用法代码示例。如果您正苦于以下问题:Python reactor.listenUDP方法的具体用法?Python reactor.listenUDP怎么用?Python reactor.listenUDP使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.reactor
的用法示例。
在下文中一共展示了reactor.listenUDP方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_startStop
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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)
示例2: test_rebind
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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)
示例3: test_badConnect
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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()
示例4: test_bindError
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [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
示例5: run_test
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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))
示例6: testRebind
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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)
示例7: setUp
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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)])
示例8: testBindError
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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
示例9: start_connection
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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
示例10: start_listener
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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
示例11: listen_udp
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
def listen_udp(udp_port):
log.info('Starting udp adapter on port "{}"'.format(udp_port))
reactor.listenUDP(udp_port, UdpAdapter())
示例12: run
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
def run(self):
"""
Run forwarding component, which actually moves data between UDP and the software bus
"""
udp_port = int(self.config['_active_']['udp_port'])
topic = unicode(self.config['_active_']['wamp_topic'])
log.info('Starting udp adapter on port "{}", publishing to topic "{}"'.format(udp_port, topic))
reactor.listenUDP(
udp_port,
UdpBusForwarder(
bus=self.bus,
topic=topic,
transform=[self.binary_decode, self.bus_encode],
logger=log))
示例13: setUpDNS
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
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)])
示例14: setUp
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
def setUp(self):
self.factory = server.DNSServerFactory([
test_domain_com, reverse_domain, my_domain_com
], verbose=2)
p = dns.DNSDatagramProtocol(self.factory)
while 1:
listenerTCP = reactor.listenTCP(0, self.factory, interface="127.0.0.1")
# It's simpler to do the stop listening with addCleanup,
# even though we might not end up using this TCP port in
# the test (if the listenUDP below fails). Cleaning up
# this TCP port sooner than "cleanup time" would mean
# adding more code to keep track of the Deferred returned
# by stopListening.
self.addCleanup(listenerTCP.stopListening)
port = listenerTCP.getHost().port
try:
listenerUDP = reactor.listenUDP(port, p, interface="127.0.0.1")
except error.CannotListenError:
pass
else:
self.addCleanup(listenerUDP.stopListening)
break
self.listenerTCP = listenerTCP
self.listenerUDP = listenerUDP
self.resolver = client.Resolver(servers=[('127.0.0.1', port)])
示例15: test_oldAddress
# 需要导入模块: from twisted.internet import reactor [as 别名]
# 或者: from twisted.internet.reactor import listenUDP [as 别名]
def test_oldAddress(self):
"""
The C{type} of the host address of a listening L{DatagramProtocol}'s
transport is C{"UDP"}.
"""
server = Server()
d = server.startedDeferred = defer.Deferred()
p = reactor.listenUDP(0, server, interface="127.0.0.1")
def cbStarted(ignored):
addr = p.getHost()
self.assertEqual(addr.type, 'UDP')
return p.stopListening()
return d.addCallback(cbStarted)