本文整理匯總了Python中socket.SocketType方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.SocketType方法的具體用法?Python socket.SocketType怎麽用?Python socket.SocketType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socket
的用法示例。
在下文中一共展示了socket.SocketType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _make_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def _make_socket(iface_name: str, can_fd: bool) -> socket.SocketType:
s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
try:
s.bind((iface_name,))
s.setsockopt(socket.SOL_SOCKET, _SO_TIMESTAMP, 1) # timestamping
if can_fd:
s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FD_FRAMES, 1)
s.setblocking(False)
if 0 != s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR):
raise OSError('Could not configure the socket: getsockopt(SOL_SOCKET, SO_ERROR) != 0')
except BaseException:
with contextlib.suppress(Exception):
s.close()
raise
return s
示例2: test_init_handles_ipv6_addr
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_init_handles_ipv6_addr(self):
addr = (
sentinel.host,
sentinel.port,
sentinel.flowinfo,
sentinel.scopeid,
)
protocol = Mock(spec=network.LineProtocol)
protocol_kwargs = {}
sock = Mock(spec=socket.SocketType)
network.Connection.__init__(
self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout
)
assert sentinel.host == self.mock.host
assert sentinel.port == self.mock.port
示例3: select_recv
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def select_recv(conn, buff_size, timeout=None):
"""add timeout for socket.recv()
:type conn: socket.SocketType
:type buff_size: int
:type timeout: float
:rtype: Union[bytes, None]
"""
rlist, _, _ = select.select([conn], [], [], timeout)
if not rlist:
# timeout
raise RuntimeError("recv timeout")
buff = conn.recv(buff_size)
if not buff:
raise RuntimeError("received zero bytes, socket was closed")
return buff
示例4: _rd_shutdown
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def _rd_shutdown(self, conn, once=False):
"""action when connection should be read-shutdown
:type conn: socket.SocketType
"""
if conn in self.conn_rd:
self.conn_rd.remove(conn)
try:
conn.shutdown(socket.SHUT_RD)
except:
pass
if not once and conn in self.map: # use the `once` param to avoid infinite loop
# if a socket is rd_shutdowned, then it's
# pair should be wr_shutdown.
self._wr_shutdown(self.map[conn], True)
if self.map.get(conn) not in self.conn_rd:
# if both two connection pair was rd-shutdowned,
# this pair sockets are regarded to be completed
# so we gonna close them
self._terminate(conn)
示例5: add_conn_pair
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def add_conn_pair(self, conn1, conn2, callback=None):
"""
transfer anything between two sockets
:type conn1: socket.SocketType
:type conn2: socket.SocketType
:param callback: callback in connection finish
:type callback: Callable
"""
# mark as readable
self.conn_rd.add(conn1)
self.conn_rd.add(conn2)
# record sockets pairs
self.map[conn1] = conn2
self.map[conn2] = conn1
# record callback
if callback is not None:
self.callbacks[conn1] = callback
示例6: test_init_ensure_nonblocking_io
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_init_ensure_nonblocking_io(self):
sock = Mock(spec=socket.SocketType)
network.Connection.__init__(
self.mock,
Mock(),
{},
sock,
(sentinel.host, sentinel.port),
sentinel.timeout,
)
sock.setblocking.assert_called_once_with(False)
示例7: test_init_stores_values_in_attributes
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_init_stores_values_in_attributes(self):
addr = (sentinel.host, sentinel.port)
protocol = Mock(spec=network.LineProtocol)
protocol_kwargs = {}
sock = Mock(spec=socket.SocketType)
network.Connection.__init__(
self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout
)
assert sock == self.mock._sock
assert protocol == self.mock.protocol
assert protocol_kwargs == self.mock.protocol_kwargs
assert sentinel.timeout == self.mock.timeout
assert sentinel.host == self.mock.host
assert sentinel.port == self.mock.port
示例8: test_stop_disables_recv_send_and_timeout
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_disables_recv_send_and_timeout(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.disable_timeout.assert_called_once_with()
self.mock.disable_recv.assert_called_once_with()
self.mock.disable_send.assert_called_once_with()
示例9: test_stop_closes_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_closes_socket(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock._sock.close.assert_called_once_with()
示例10: test_stop_stops_actor
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_stops_actor(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.actor_ref.stop.assert_called_once_with(block=False)
示例11: test_stop_handles_actor_already_being_stopped
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_handles_actor_already_being_stopped(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock.actor_ref.stop.side_effect = pykka.ActorDeadError()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
self.mock.actor_ref.stop.assert_called_once_with(block=False)
示例12: test_stop_sets_stopping_to_true
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_sets_stopping_to_true(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
assert self.mock.stopping is True
示例13: test_stop_does_not_proceed_when_already_stopping
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_does_not_proceed_when_already_stopping(self):
self.mock.stopping = True
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
assert 0 == self.mock.actor_ref.stop.call_count
assert 0 == self.mock._sock.close.call_count
示例14: test_stop_logs_reason
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_logs_reason(self):
self.mock.stopping = False
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
network.logger.log.assert_called_once_with(
logging.DEBUG, sentinel.reason
)
示例15: test_stop_logs_that_it_is_calling_itself
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SocketType [as 別名]
def test_stop_logs_that_it_is_calling_itself(self):
self.mock.stopping = True
self.mock.actor_ref = Mock()
self.mock._sock = Mock(spec=socket.SocketType)
network.Connection.stop(self.mock, sentinel.reason)
network.logger.log(any_int, any_unicode)