本文整理匯總了Python中socket.TCP_KEEPIDLE屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.TCP_KEEPIDLE屬性的具體用法?Python socket.TCP_KEEPIDLE怎麽用?Python socket.TCP_KEEPIDLE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.TCP_KEEPIDLE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: start
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [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: _connect
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def _connect(cls, host, port, family = socket.AF_INET, socktype = socket.SOCK_STREAM,
proto = 0, timeout = 3, nodelay = False, keepalive = False):
family, socktype, proto, _, sockaddr = socket.getaddrinfo(host, port, family,
socktype, proto)[0]
s = socket.socket(family, socktype, proto)
s.settimeout(timeout)
s.connect(sockaddr)
if nodelay:
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if keepalive:
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# Linux specific: after 10 idle minutes, start sending keepalives every 5 minutes.
# Drop connection after 10 failed keepalives
if hasattr(socket, "TCP_KEEPIDLE") and hasattr(socket, "TCP_KEEPINTVL") and hasattr(socket, "TCP_KEEPCNT"):
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10 * 60)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 5 * 60)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 10)
return s
示例3: test_connect_tcp_keepalive_options
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def test_connect_tcp_keepalive_options(event_loop):
conn = Connection(
loop=event_loop,
socket_keepalive=True,
socket_keepalive_options={
socket.TCP_KEEPIDLE: 1,
socket.TCP_KEEPINTVL: 1,
socket.TCP_KEEPCNT: 3,
},
)
await conn._connect()
sock = conn._writer.transport.get_extra_info('socket')
assert sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) == 1
for k, v in (
(socket.TCP_KEEPIDLE, 1),
(socket.TCP_KEEPINTVL, 1),
(socket.TCP_KEEPCNT, 3),
):
assert sock.getsockopt(socket.SOL_TCP, k) == v
conn.disconnect()
示例4: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def __init__(self, **kwargs):
socket_options = kwargs.pop('socket_options',
SocketOptionsAdapter.default_options)
idle = kwargs.pop('idle', 60)
interval = kwargs.pop('interval', 20)
count = kwargs.pop('count', 5)
socket_options = socket_options + [
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval),
(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, count),
]
# NOTE(Ian): Apparently OSX does not have this constant defined, so we
# set it conditionally.
if getattr(socket, 'TCP_KEEPIDLE', None) is not None:
socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle)]
super(TCPKeepAliveAdapter, self).__init__(
socket_options=socket_options, **kwargs
)
示例5: test_socket_options_for_simple_server
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def test_socket_options_for_simple_server(self):
# test normal socket options has set properly
self.flags(tcp_keepidle=500, group='wsgi')
server = masakari.wsgi.Server("test_socket_options", None,
host="127.0.0.1", port=0)
server.start()
sock = server._socket
self.assertEqual(1, sock.getsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR))
self.assertEqual(1, sock.getsockopt(socket.SOL_SOCKET,
socket.SO_KEEPALIVE))
if hasattr(socket, 'TCP_KEEPIDLE'):
self.assertEqual(CONF.wsgi.tcp_keepidle,
sock.getsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPIDLE))
server.stop()
server.wait()
示例6: test_socket_options_for_simple_server
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def test_socket_options_for_simple_server(self):
# test normal socket options has set properly
self.config(tcp_keepidle=500)
server = wsgi.Server(self.conf, "test_socket_options", None,
host="127.0.0.1", port=0)
server.start()
sock = server.socket
self.assertEqual(1, sock.getsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR))
self.assertEqual(1, sock.getsockopt(socket.SOL_SOCKET,
socket.SO_KEEPALIVE))
if hasattr(socket, 'TCP_KEEPIDLE'):
self.assertEqual(self.conf.tcp_keepidle,
sock.getsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPIDLE))
self.assertFalse(server._server.dead)
server.stop()
server.wait()
self.assertTrue(server._server.dead)
示例7: test_socket_options_for_ssl_server
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def test_socket_options_for_ssl_server(self):
# test normal socket options has set properly
self.config(tcp_keepidle=500)
server = wsgi.Server(self.conf, "test_socket_options", None,
host="127.0.0.1", port=0, use_ssl=True)
server.start()
sock = server.socket
self.assertEqual(1, sock.getsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR))
self.assertEqual(1, sock.getsockopt(socket.SOL_SOCKET,
socket.SO_KEEPALIVE))
if hasattr(socket, 'TCP_KEEPIDLE'):
self.assertEqual(CONF.tcp_keepidle,
sock.getsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPIDLE))
server.stop()
server.wait()
示例8: start
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def start(self, key=None, backlog=128):
"""Run a WSGI server with the given application."""
if self.socket is None:
self.listen(key=key, backlog=backlog)
dup_socket = self.socket.dup()
if key:
self.socket_info[key] = self.socket.getsockname()
# Optionally enable keepalive on the wsgi socket.
if self.keepalive:
dup_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if self.keepidle is not None:
dup_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE,
self.keepidle)
self.greenthread = self.pool.spawn(self._run,
self.application,
dup_socket)
示例9: test_correct_configure_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def test_correct_configure_socket(self):
mock_socket = mock.Mock()
self.useFixture(fixtures.MonkeyPatch(
'senlin.api.common.wsgi.ssl.wrap_socket',
mock_socket))
self.useFixture(fixtures.MonkeyPatch(
'senlin.api.common.wsgi.eventlet.listen',
lambda *x, **y: mock_socket))
server = wsgi.Server(name='senlin-api', conf=cfg.CONF.senlin_api)
server.default_port = 1234
server.configure_socket()
self.assertIn(mock.call.setsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR, 1),
mock_socket.mock_calls)
self.assertIn(mock.call.setsockopt(socket.SOL_SOCKET,
socket.SO_KEEPALIVE, 1),
mock_socket.mock_calls)
if hasattr(socket, 'TCP_KEEPIDLE'):
self.assertIn(mock.call().setsockopt(
socket.IPPROTO_TCP,
socket.TCP_KEEPIDLE,
wsgi.cfg.CONF.senlin_api.tcp_keepidle), mock_socket.mock_calls)
示例10: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def __init__(self, ip, port, callbacks_info, max_client = 50):
assert (ip != '')
skt = socket.socket()
# Avoid 'Address already in use' error when trying to lanch server
# again right after shutting it down.
skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
skt.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if sys.platform == 'linux':
skt.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 60)
skt.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 4)
skt.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 15)
skt.bind((ip, port))
skt.listen(max_client)
self.connections = []
self.clients = {}
self.msg_handler = MessageHandler(skt, callbacks_info,
mh_creator = self.client_mh_creator,
mh_remover = self.client_mh_remover)
self.thread = TaskThread(name='server_recv_loop')
self.thread.daemon = True
self.evt_break = threading.Event()
self.evt_break.clear()
示例11: configure_timeouts
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def configure_timeouts(self, loop_wait, retry_timeout, ttl):
# Normally every loop_wait seconds we should have receive something from the socket.
# If we didn't received anything after the loop_wait + retry_timeout it is a time
# to start worrying (send keepalive messages). Finally, the connection should be
# considered as dead if we received nothing from the socket after the ttl seconds.
cnt = 3
idle = int(loop_wait + retry_timeout)
intvl = max(1, int(float(ttl - idle) / cnt))
self._api.api_client.rest_client.pool_manager.connection_pool_kw['socket_options'] = [
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle),
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, intvl),
(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, cnt),
(socket.IPPROTO_TCP, 18, int(ttl * 1000)) # TCP_USER_TIMEOUT
]
self._request_timeout = (1, retry_timeout / 3.0)
示例12: set_keepalive
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def set_keepalive(sock, after_idle_sec=1, interval_sec=3, max_fails=5):
"""Set TCP keepalive on an open socket.
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
"""
if hasattr(socket, "SO_KEEPALIVE"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if hasattr(socket, "TCP_KEEPIDLE"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, after_idle_sec)
if hasattr(socket, "TCP_KEEPINTVL"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval_sec)
if hasattr(socket, "TCP_KEEPCNT"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails)
示例13: _set_socket_options
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def _set_socket_options(sock: anyio.SocketStream):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if hasattr(socket, "TCP_KEEPIDLE"):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 300)
elif is_darwin():
# Darwin specific option
TCP_KEEPALIVE = 16
sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPALIVE, 300)
if not is_windows():
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
示例14: set_keepalive_linux
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def set_keepalive_linux(sock, after_idle_sec=3600, interval_sec=3, max_fails=5):
"""Set TCP keepalive on an open socket.
It activates after 3600 seconds (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
"""
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)
示例15: set_keepalive
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import TCP_KEEPIDLE [as 別名]
def set_keepalive(sock, interval=40, attempts=5):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if hasattr(socket, "TCP_KEEPIDLE"):
try_setsockopt(sock, socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, interval)
if hasattr(socket, "TCP_KEEPINTVL"):
try_setsockopt(sock, socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)
if hasattr(socket, "TCP_KEEPCNT"):
try_setsockopt(sock, socket.IPPROTO_TCP, socket.TCP_KEEPCNT, attempts)