本文整理汇总了Python中socket.MSG_DONTWAIT属性的典型用法代码示例。如果您正苦于以下问题:Python socket.MSG_DONTWAIT属性的具体用法?Python socket.MSG_DONTWAIT怎么用?Python socket.MSG_DONTWAIT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.MSG_DONTWAIT属性的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_flags
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def test_flags(self):
"""
The C{flags} argument to L{send1msg} is passed on to the underlying
C{sendmsg} call, to affect it in whatever way is defined by those
flags.
"""
# Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT
# makes the send a non-blocking call, even if the socket is in blocking
# mode. See also test_flags in RecvmsgTests
for i in range(1024):
try:
send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT)
except error as e:
self.assertEqual(e.args[0], errno.EAGAIN)
break
else:
self.fail(
"Failed to fill up the send buffer, "
"or maybe send1msg blocked for a while")
示例2: sendto
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def sendto(self, msg, address):
if (self.sock.getsockname(), address) not in self.isolations:
self.lg.debug("{} - [{}] --> {}".format(self.sock.getsockname(), msg, address))
try:
return self.sock.sendto(msg, socket.MSG_DONTWAIT, address)
except ConnectionRefusedError:
self.lg.warning(
"simulator_stuff.sendto: the message {} has not been \
delivered because the destination {} left the team".format(
msg, address))
except KeyboardInterrupt:
self.lg.warning("simulator_stuff.sendto: send_packet {} to {}".format(msg, address))
raise
except FileNotFoundError:
self.lg.error("simulator_stuff.sendto: {}".format(address))
raise
except BlockingIOError:
raise
else:
self.lg.warning("{} not sent from {} to {} (isolated)".format(msg, self.sock.getsockname(), address))
示例3: test_flags
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def test_flags(self):
"""
The C{flags} argument to L{send1msg} is passed on to the underlying
C{sendmsg} call, to affect it in whatever way is defined by those
flags.
"""
# Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT
# makes the send a non-blocking call, even if the socket is in blocking
# mode. See also test_flags in RecvmsgTests
for i in range(8 * 1024):
try:
send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT)
except error as e:
self.assertEqual(e.args[0], errno.EAGAIN)
break
else:
self.fail(
"Failed to fill up the send buffer, "
"or maybe send1msg blocked for a while")
示例4: _read
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def _read(self):
#read until the server is stopped or the client disconnects
while self._client_connected:
#read data from Bluetooth socket
try:
data = self._client_sock.recv(1024, socket.MSG_DONTWAIT)
except IOError as e:
self._handle_bt_error(e)
data = b""
if data:
if self._data_received_callback:
if self._encoding:
data = data.decode(self._encoding)
self.data_received_callback(data)
if self._conn_thread.stopping.wait(BLUETOOTH_TIMEOUT):
break
#close the client socket
self._client_sock.close()
self._client_sock = None
self._client_info = None
self._client_connected = False
示例5: read_ha_proxy_stats
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def read_ha_proxy_stats(haproxy_stats_socket):
conn = socket(AF_UNIX, SOCK_STREAM)
try:
conn.connect(haproxy_stats_socket)
conn.sendall(b'show stat\r\n')
data = conn.recv(BUFFER_SIZE)
while len(data) % BUFFER_SIZE == 0:
try:
data += conn.recv(BUFFER_SIZE, MSG_DONTWAIT)
except socket.error:
break
return data
finally:
conn.close()
示例6: sendto
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def sendto(self, msg, address):
self.lg.debug(f"{self.sock.getsockname()} - [{msg}] --> {address}")
try:
return self.sock.sendto(msg, socket.MSG_DONTWAIT, address)
except ConnectionRefusedError:
self.lg.error("sendto: connection refused from {address}")
except KeyboardInterrupt:
self.lg.warning("sendto: keyboard interrupt")
raise
except FileNotFoundError:
self.lg.error("sendto: file not found")
raise
except BlockingIOError:
raise
示例7: _send
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def _send(self, sock, data):
sock.sendall(data, socket.MSG_DONTWAIT)
示例8: _testSendmsgTimeout
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def _testSendmsgTimeout(self):
try:
self.cli_sock.settimeout(0.03)
with self.assertRaises(socket.timeout):
while True:
self.sendmsgToServer([b"a"*512])
finally:
self.misc_event.set()
# XXX: would be nice to have more tests for sendmsg flags argument.
# Linux supports MSG_DONTWAIT when sending, but in general, it
# only works when receiving. Could add other platforms if they
# support it too.
示例9: testSendmsgDontWait
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def testSendmsgDontWait(self):
# Check that MSG_DONTWAIT in flags causes non-blocking behaviour.
self.assertEqual(self.serv_sock.recv(512), b"a"*512)
self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout))
示例10: _testSendmsgDontWait
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def _testSendmsgDontWait(self):
try:
with self.assertRaises(OSError) as cm:
while True:
self.sendmsgToServer([b"a"*512], [], socket.MSG_DONTWAIT)
self.assertIn(cm.exception.errno,
(errno.EAGAIN, errno.EWOULDBLOCK))
finally:
self.misc_event.set()
示例11: process
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def process(self):
"""Without blocking, read available packets and invoke their callbacks."""
data = self.socket.recv(BUFFER_SIZE, socket.MSG_DONTWAIT)
buf = ctypes.create_string_buffer(data)
nfq.nfq_handle_packet(self.handle, buf, len(data))
示例12: try_recv
# 需要导入模块: import socket [as 别名]
# 或者: from socket import MSG_DONTWAIT [as 别名]
def try_recv(self):
"""Return None immediately if nothing is waiting"""
try:
lenstr = self.sock.recv(4, socket.MSG_DONTWAIT)
except socket.error:
return None
if len(lenstr) < 4:
raise EOFError("Socket closed")
length = struct.unpack("<I", lenstr)[0]
return self._get_next_obj(length)