本文整理匯總了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)