本文整理汇总了Python中socket.CMSG_SPACE属性的典型用法代码示例。如果您正苦于以下问题:Python socket.CMSG_SPACE属性的具体用法?Python socket.CMSG_SPACE怎么用?Python socket.CMSG_SPACE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.CMSG_SPACE属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCMSG_SPACE
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testCMSG_SPACE(self):
# Test CMSG_SPACE() with various valid and invalid values,
# checking the assumptions used by sendmsg().
toobig = self.socklen_t_limit - socket.CMSG_SPACE(1) + 1
values = list(range(257)) + list(range(toobig - 257, toobig))
last = socket.CMSG_SPACE(0)
# struct cmsghdr has at least three members, two of which are ints
self.assertGreater(last, array.array("i").itemsize * 2)
for n in values:
ret = socket.CMSG_SPACE(n)
self.assertGreaterEqual(ret, last)
self.assertGreaterEqual(ret, socket.CMSG_LEN(n))
self.assertGreaterEqual(ret, n + socket.CMSG_LEN(0))
self.assertLessEqual(ret, self.socklen_t_limit)
last = ret
self.assertRaises(OverflowError, socket.CMSG_SPACE, -1)
# sendmsg() shares code with these functions, and requires
# that it reject values over the limit.
self.assertRaises(OverflowError, socket.CMSG_SPACE, toobig)
self.assertRaises(OverflowError, socket.CMSG_SPACE, sys.maxsize)
示例2: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def __init__(self, iface_name: str, mtu: int, loop: typing.Optional[asyncio.AbstractEventLoop] = None) -> None:
"""
CAN Classic/FD is selected automatically based on the MTU. It is not possible to use CAN FD with MTU of 8 bytes.
:param iface_name: E.g., ``can0``.
:param mtu: The maximum data field size in bytes. CAN FD is used if this value > 8, Classic CAN otherwise.
This value must belong to Media.VALID_MTU_SET.
:param loop: The event loop to use. Defaults to :func:`asyncio.get_event_loop`.
"""
self._mtu = int(mtu)
if self._mtu not in self.VALID_MTU_SET:
raise ValueError(f'Invalid MTU: {self._mtu} not in {self.VALID_MTU_SET}')
self._iface_name = str(iface_name)
self._loop = loop if loop is not None else asyncio.get_event_loop()
self._is_fd = self._mtu > _NativeFrameDataCapacity.CAN_CLASSIC
self._native_frame_data_capacity = int({
False: _NativeFrameDataCapacity.CAN_CLASSIC,
True: _NativeFrameDataCapacity.CAN_FD,
}[self._is_fd])
self._native_frame_size = _FRAME_HEADER_STRUCT.size + self._native_frame_data_capacity
self._sock = _make_socket(iface_name, can_fd=self._is_fd)
self._closed = False
self._maybe_thread: typing.Optional[threading.Thread] = None
self._loopback_enabled = False
self._ancillary_data_buffer_size = socket.CMSG_SPACE(_TIMEVAL_STRUCT.size) # Used for recvmsg()
super(SocketCANMedia, self).__init__()
示例3: recvmsg
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def recvmsg(socket, maxSize=8192, cmsgSize=4096, flags=0):
"""
Receive a message on a socket.
@param socket: The socket to receive the message on.
@type socket: L{socket.socket}
@param maxSize: The maximum number of bytes to receive from the socket using
the datagram or stream mechanism. The default maximum is 8192.
@type maxSize: L{int}
@param cmsgSize: The maximum number of bytes to receive from the socket
outside of the normal datagram or stream mechanism. The default maximum
is 4096.
@type cmsgSize: L{int}
@param flags: Flags to affect how the message is sent. See the C{MSG_}
constants in the sendmsg(2) manual page. By default no flags are set.
@type flags: L{int}
@return: A named 3-tuple of the bytes received using the datagram/stream
mechanism, a L{list} of L{tuple}s giving ancillary received data, and
flags as an L{int} describing the data received.
"""
if _PY3:
# In Twisted's sendmsg.c, the csmg_space is defined as:
# int cmsg_size = 4096;
# cmsg_space = CMSG_SPACE(cmsg_size);
# Since the default in Python 3's socket is 0, we need to define our
# own default of 4096. -hawkie
data, ancillary, flags = socket.recvmsg(
maxSize, CMSG_SPACE(cmsgSize), flags)[0:3]
else:
data, flags, ancillary = recv1msg(
socket.fileno(), flags, maxSize, cmsgSize)
return RecievedMessage(data=data, ancillary=ancillary, flags=flags)
示例4: _testSendmsgExcessCmsgReject
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def _testSendmsgExcessCmsgReject(self):
if not hasattr(socket, "CMSG_SPACE"):
# Can only send one item
with self.assertRaises(OSError) as cm:
self.sendmsgToServer([MSG], [(0, 0, b""), (0, 0, b"")])
self.assertIsNone(cm.exception.errno)
self.sendToServer(b"done")
示例5: testFDPassCMSG_SPACE
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testFDPassCMSG_SPACE(self):
# Test using CMSG_SPACE() to calculate ancillary buffer size.
self.checkRecvmsgFDs(
4, self.doRecvmsg(self.serv_sock, len(MSG),
socket.CMSG_SPACE(4 * SIZEOF_INT)))
示例6: testFDPassSeparateMinSpace
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testFDPassSeparateMinSpace(self):
# Pass two FDs in two separate arrays, receiving them into the
# minimum space for two arrays.
self.checkRecvmsgFDs(2,
self.doRecvmsg(self.serv_sock, len(MSG),
socket.CMSG_SPACE(SIZEOF_INT) +
socket.CMSG_LEN(SIZEOF_INT)),
maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC)
示例7: testRecvHopLimitCMSG_SPACE
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testRecvHopLimitCMSG_SPACE(self):
# Test receiving hop limit, using CMSG_SPACE to calculate buffer size.
self.checkHopLimit(ancbufsize=socket.CMSG_SPACE(SIZEOF_INT))
示例8: testSecondCmsgTrunc0
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testSecondCmsgTrunc0(self):
self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT),
ignoreflags=socket.MSG_CTRUNC)
示例9: testSecondCmsgTrunc1
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testSecondCmsgTrunc1(self):
self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 1)
示例10: testSecondCmsgTrunc2Int
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testSecondCmsgTrunc2Int(self):
self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) +
2 * SIZEOF_INT)
示例11: testSecondCmsgTruncLen0Minus1
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testSecondCmsgTruncLen0Minus1(self):
self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) +
socket.CMSG_LEN(0) - 1)
示例12: testSecomdCmsgTruncInData
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def testSecomdCmsgTruncInData(self):
# Test truncation of the second of two control messages inside
# its associated data.
self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
socket.IPV6_RECVHOPLIMIT, 1)
self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
socket.IPV6_RECVTCLASS, 1)
self.misc_event.set()
msg, ancdata, flags, addr = self.doRecvmsg(
self.serv_sock, len(MSG),
socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1)
self.assertEqual(msg, MSG)
self.checkRecvmsgAddress(addr, self.cli_addr)
self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC)
cmsg_types = {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT}
cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0)
self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
cmsg_types.remove(cmsg_type)
self.assertEqual(len(cmsg_data), SIZEOF_INT)
a = array.array("i")
a.frombytes(cmsg_data)
self.assertGreaterEqual(a[0], 0)
self.assertLessEqual(a[0], 255)
if ancdata:
cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0)
self.assertEqual(cmsg_level, socket.IPPROTO_IPV6)
cmsg_types.remove(cmsg_type)
self.assertLess(len(cmsg_data), SIZEOF_INT)
self.assertEqual(ancdata, [])
示例13: recvfds
# 需要导入模块: import socket [as 别名]
# 或者: from socket import CMSG_SPACE [as 别名]
def recvfds(sock, size):
'''Receive an array of fds over an AF_UNIX socket.'''
a = array.array('i')
bytes_size = a.itemsize * size
msg, ancdata, flags, addr = sock.recvmsg(1, socket.CMSG_SPACE(bytes_size))
if not msg and not ancdata:
raise EOFError
try:
if ACKNOWLEDGE:
sock.send(b'A')
if len(ancdata) != 1:
raise RuntimeError('received %d items of ancdata' %
len(ancdata))
cmsg_level, cmsg_type, cmsg_data = ancdata[0]
if (cmsg_level == socket.SOL_SOCKET and
cmsg_type == socket.SCM_RIGHTS):
if len(cmsg_data) % a.itemsize != 0:
raise ValueError
a.frombytes(cmsg_data)
if len(a) % 256 != msg[0]:
raise AssertionError(
"Len is {0:n} but msg[0] is {1!r}".format(
len(a), msg[0]))
return list(a)
except (ValueError, IndexError):
pass
raise RuntimeError('Invalid data received')