本文整理汇总了Python中socket.SIO_KEEPALIVE_VALS属性的典型用法代码示例。如果您正苦于以下问题:Python socket.SIO_KEEPALIVE_VALS属性的具体用法?Python socket.SIO_KEEPALIVE_VALS怎么用?Python socket.SIO_KEEPALIVE_VALS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.SIO_KEEPALIVE_VALS属性的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SIO_KEEPALIVE_VALS [as 别名]
def start(self):
family, socktype, proto, _, sockaddr = socket.getaddrinfo(self.host, self.port, self.family, self.type)[0]
sock = socket.socket(family, socktype)
sock.connect(sockaddr)
if self.nodelay:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if self.keepalive:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if hasattr(socket, "TCP_KEEPIDLE") and hasattr(socket, "TCP_KEEPINTVL") and hasattr(socket, "TCP_KEEPCNT"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1 * 60)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
elif hasattr(socket, "SIO_KEEPALIVE_VALS"):
sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 1 * 60 * 1000, 5 * 60 * 1000))
self.sock = sock
self.recv_count = 0
Ksecurity().reset_aes()
示例2: set_keep_alive
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SIO_KEEPALIVE_VALS [as 别名]
def set_keep_alive(self, sock, after_idle_sec=5, interval_sec=60,
max_fails=5):
"""
This function instructs the TCP socket to send a heart beat every n
seconds to detect dead connections. It's the TCP equivalent of the
IRC ping-pong protocol and allows for better cleanup / detection
of dead TCP connections.
It activates after 1 second (after_idle_sec) of idleness, then sends
a keepalive ping once every 3 seconds(interval_sec), and closes the
connection after 5 failed ping (max_fails), or 15 seconds
"""
# OSX
if platform.system() == "Darwin":
# scraped from /usr/include, not exported by python's socket module
TCP_KEEPALIVE = 0x10
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPALIVE, interval_sec)
if platform.system() == "Windows":
sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 10000, 3000))
if platform.system() == "Linux":
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE,
after_idle_sec)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL,
interval_sec)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails)
示例3: create_outbound_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SIO_KEEPALIVE_VALS [as 别名]
def create_outbound_socket(self, address):
address = socket.getaddrinfo(*address)[0]
server_sock = socket.socket(*address[:3])
if self.timeout:
server_sock.settimeout(self.timeout)
server_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
server_sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 60000, 2000))
server_sock = ssl.wrap_socket(server_sock)
return server_sock
示例4: test_sock_ioctl
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SIO_KEEPALIVE_VALS [as 别名]
def test_sock_ioctl(self):
self.assertTrue(hasattr(socket.socket, 'ioctl'))
self.assertTrue(hasattr(socket, 'SIO_RCVALL'))
self.assertTrue(hasattr(socket, 'RCVALL_ON'))
self.assertTrue(hasattr(socket, 'RCVALL_OFF'))
self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS'))
s = socket.socket()
self.addCleanup(s.close)
self.assertRaises(ValueError, s.ioctl, -1, None)
s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
示例5: _set_keepalive_times
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SIO_KEEPALIVE_VALS [as 别名]
def _set_keepalive_times(sock):
idle_ms = min(_DEFAULT_TCP_IDLE_MS, _MAX_TCP_KEEPIDLE * 1000)
interval_ms = min(_DEFAULT_TCP_INTERVAL_MS,
_MAX_TCP_KEEPINTVL * 1000)
if (idle_ms < _DEFAULT_TCP_IDLE_MS or
interval_ms < _DEFAULT_TCP_INTERVAL_MS):
sock.ioctl(socket.SIO_KEEPALIVE_VALS,
(1, idle_ms, interval_ms))
示例6: connect_to_server
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SIO_KEEPALIVE_VALS [as 别名]
def connect_to_server(self):
"""
Attempts to connect to the TCP server.
Raises:
Exception (OSException): The connection has failed.
"""
try:
self.tcp.connect((self.address, TCP_PORT))
# self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # turn on the TCP keepalive
# self.tcp.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 1000, 1000)) # configure to send a keepalive packet every second
except Exception:
self.logger.exception("Unable to connect to host device: IP {0} did not respond.".format(self.address))
sys.exit(1)