本文整理汇总了Python中twisted.internet.protocol.DatagramProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python protocol.DatagramProtocol方法的具体用法?Python protocol.DatagramProtocol怎么用?Python protocol.DatagramProtocol使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.protocol
的用法示例。
在下文中一共展示了protocol.DatagramProtocol方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_invalidDescriptor
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def test_invalidDescriptor(self):
"""
An implementation of L{IReactorSocket.adoptDatagramPort} raises
L{socket.error} if passed an integer which is not associated with a
socket.
"""
reactor = self.buildReactor()
probe = socket.socket()
fileno = probe.fileno()
probe.close()
exc = self.assertRaises(
socket.error,
reactor.adoptDatagramPort, fileno, socket.AF_INET,
DatagramProtocol())
if platform.isWindows() and _PY3:
self.assertEqual(exc.args[0], errno.WSAENOTSOCK)
else:
self.assertEqual(exc.args[0], errno.EBADF)
示例2: test_invalidAddressFamily
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def test_invalidAddressFamily(self):
"""
An implementation of L{IReactorSocket.adoptDatagramPort} raises
L{UnsupportedAddressFamily} if passed an address family it does not
support.
"""
reactor = self.buildReactor()
port = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.addCleanup(port.close)
arbitrary = 2 ** 16 + 7
self.assertRaises(
UnsupportedAddressFamily,
reactor.adoptDatagramPort, port.fileno(), arbitrary,
DatagramProtocol())
示例3: test_startStop
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [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)
示例4: test_rebind
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [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)
示例5: __init__
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def __init__(self, _parent, _name, _config, _logger, _port):
self._parent = _parent
self._logger = _logger
self._config = _config
self._system = _name
self._gateways = [(self._parent._gateway, self._parent._gateway_port)]
self._ambeRxPort = _port # Port to listen on for AMBE frames to transmit to all peers
self._dmrgui = '127.0.0.1'
self._sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self._slot = 2 # "current slot"
self.rx = [0, RX_SLOT(1, 0, 0, 0, 1), RX_SLOT(2, 0, 0, 0, 1)]
self.tx = [0, TX_SLOT(1, 0, 0, 0, 1), TX_SLOT(2, 0, 0, 0, 1)]
class UDP_IMPORT(DatagramProtocol):
def __init__(self, callback_function):
self.func = callback_function
def datagramReceived(self, _data, (_host, _port)):
self.func(_data, (_host, _port))
示例6: getService
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def getService(self):
"""Return service to be run
This handles the easy case where the CanaryService class is
also the Factory/Datagram class. Subclasses should override
this if more intricracy is needed.
"""
if isinstance(self, Factory):
return internet.TCPServer(self.port, self)
elif isinstance(self, DatagramProtocol):
return internet.UDPServer(self.port, self)
err = 'The class %s does not inherit from either Factory or DatagramProtocol.' % (
self.__class__.__name__
)
raise Exception(err)
示例7: test_UDP
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
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)
示例8: testRebind
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [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)
示例9: test_stopOnlyCloses
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def test_stopOnlyCloses(self):
"""
When the L{IListeningPort} returned by
L{IReactorSocket.adoptDatagramPort} is stopped using
C{stopListening}, the underlying socket is closed but not
shutdown. This allows another process which still has a
reference to it to continue reading and writing to it.
"""
reactor = self.buildReactor()
portSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.addCleanup(portSocket.close)
portSocket.bind(("127.0.0.1", 0))
portSocket.setblocking(False)
# The file descriptor is duplicated by adoptDatagramPort
port = reactor.adoptDatagramPort(
portSocket.fileno(), portSocket.family, DatagramProtocol())
d = port.stopListening()
def stopped(ignored):
# Should still be possible to recv on portSocket. If
# it was shutdown, the exception would be EINVAL instead.
exc = self.assertRaises(socket.error, portSocket.recvfrom, 1)
if platform.isWindows() and _PY3:
self.assertEqual(exc.args[0], errno.WSAEWOULDBLOCK)
else:
self.assertEqual(exc.args[0], errno.EAGAIN)
d.addCallback(stopped)
d.addErrback(err, "Failed to read on original port.")
needsRunningReactor(
reactor,
lambda: d.addCallback(lambda ignored: reactor.stop()))
reactor.run()
示例10: test_listenMode
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def test_listenMode(self):
"""
The UNIX socket created by L{IReactorUNIXDatagram.listenUNIXDatagram}
is created with the mode specified.
"""
self._modeTest('listenUNIXDatagram', self.mktemp(), DatagramProtocol())
示例11: test_listenOnLinuxAbstractNamespace
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def test_listenOnLinuxAbstractNamespace(self):
"""
On Linux, a UNIX socket path may begin with C{'\0'} to indicate a socket
in the abstract namespace. L{IReactorUNIX.listenUNIXDatagram} accepts
such a path.
"""
path = _abstractPath(self)
reactor = self.buildReactor()
port = reactor.listenUNIXDatagram('\0' + path, DatagramProtocol())
self.assertEqual(port.getHost(), UNIXAddress('\0' + path))
示例12: test_oldAddress
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [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)
示例13: testUDP
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
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()
示例14: addProto
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def addProto(self, num, proto):
if not isinstance(proto, protocol.DatagramProtocol):
raise TypeError('Added protocol must be an instance of DatagramProtocol')
if num < 0:
raise TypeError('Added protocol must be positive or zero')
if num >= 2**16:
raise TypeError('Added protocol must fit in 16 bits')
if num not in self.udpProtos:
self.udpProtos[num] = []
self.udpProtos[num].append(proto)
示例15: testAddingBadProtos_WrongLevel
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import DatagramProtocol [as 别名]
def testAddingBadProtos_WrongLevel(self):
"""Adding a wrong level protocol raises an exception."""
e = rawudp.RawUDPProtocol()
try:
e.addProto(42, "silliness")
except TypeError as e:
if e.args == ('Added protocol must be an instance of DatagramProtocol',):
pass
else:
raise
else:
raise AssertionError('addProto must raise an exception for bad protocols')