本文整理汇总了Python中socket.TCP_KEEPINTVL属性的典型用法代码示例。如果您正苦于以下问题:Python socket.TCP_KEEPINTVL属性的具体用法?Python socket.TCP_KEEPINTVL怎么用?Python socket.TCP_KEEPINTVL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.TCP_KEEPINTVL属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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_KEEPINTVL [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_KEEPINTVL [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_KEEPINTVL [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: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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()
示例6: configure_timeouts
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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)
示例7: set_keepalive
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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)
示例8: _set_socket_options
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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)
示例9: set_keepalive_linux
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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)
示例10: set_keepalive
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [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)
示例11: accept_connections
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [as 别名]
def accept_connections(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
""" Set TCP keepalive on an open socket.
It activates after 1 second (after_idle_sec) of idleness,
then sends a keepalive ping once every 1 seconds (interval_sec),
and closes the connection after 100 failed ping (max_fails)
"""
#self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# pylint: disable=no-member
if hasattr(socket, 'TCP_KEEPIDLE'):
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
elif hasattr(socket, 'TCP_KEEPALIVE'):
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, 1)
if hasattr(socket, 'TCP_KEEPINTVL'):
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 1)
if hasattr(socket, 'TCP_KEEPCNT'):
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 100)
# pylint: enable=no-member
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
self.sock.bind((self.bind_addr, self.bind_port))
self.sock.listen(1)
logger.info('Listening on {}:{}.'.format(self.bind_addr, self.bind_port))
try:
while True:
conn, addr = self.sock.accept()
logger.info("Received a connection, creating an ADBConnection.")
thread = threading.Thread(target=ADBConnection, args=(conn, addr))
thread.daemon = True
thread.start()
except KeyboardInterrupt:
logger.info('Exiting...')
self.sock.close()
if output_writer:
output_writer.stop()
示例12: _open_connection
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [as 别名]
def _open_connection(self) -> None:
"""
Opens a connection to the GPSD server and configures the TCP socket.
"""
self.logger.info(
f"Connecting to gpsd at {self.connection_args['host']}" +
(f":{self.connection_args['port']}"
if self.connection_args['port'] else ''))
self.reader, self.writer = await asyncio.wait_for(
asyncio.open_connection(**self.connection_args),
self.connection_timeout,
loop=self.loop)
# Set socket options
sock = self.writer.get_extra_info('socket')
if sock is not None:
if 'SO_KEEPALIVE' in self.alive_opts:
sock.setsockopt(socket.SOL_SOCKET,
socket.SO_KEEPALIVE,
self.alive_opts['SO_KEEPALIVE'])
if hasattr(
sock,
'TCP_KEEPIDLE') and 'TCP_KEEPIDLE' in self.alive_opts:
sock.setsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPIDLE, # pylint: disable=E1101
self.alive_opts['TCP_KEEPIDLE'])
if hasattr(
sock,
'TCP_KEEPINTVL') and 'TCP_KEEPINTVL' in self.alive_opts:
sock.setsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPINTVL, # pylint: disable=E1101
self.alive_opts['TCP_KEEPINTVL'])
if hasattr(
sock,
'TCP_KEEPCNT') and 'TCP_KEEPCNT' in self.alive_opts:
sock.setsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPCNT,
self.alive_opts['TCP_KEEPCNT'])
示例13: start_socket_client
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [as 别名]
def start_socket_client(self, host, port):
self._sequence = -1
if SHOW_WRITES_AND_READS:
print("Connecting to %s:%s" % (host, port))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Set TCP keepalive on an open socket.
# It activates after 1 second (TCP_KEEPIDLE,) of idleness,
# then sends a keepalive ping once every 3 seconds (TCP_KEEPINTVL),
# and closes the connection after 5 failed ping (TCP_KEEPCNT), or 15 seconds
try:
from socket import IPPROTO_TCP, SO_KEEPALIVE, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT
s.setsockopt(socket.SOL_SOCKET, SO_KEEPALIVE, 1)
s.setsockopt(IPPROTO_TCP, TCP_KEEPIDLE, 1)
s.setsockopt(IPPROTO_TCP, TCP_KEEPINTVL, 3)
s.setsockopt(IPPROTO_TCP, TCP_KEEPCNT, 5)
except ImportError:
pass # May not be available everywhere.
# 10 seconds default timeout
timeout = int(os.environ.get('PYDEVD_CONNECT_TIMEOUT', 10))
s.settimeout(timeout)
for _i in range(20):
try:
s.connect((host, port))
break
except:
time.sleep(.5) # We may have to wait a bit more and retry (especially on PyPy).
s.settimeout(None) # no timeout after connected
if SHOW_WRITES_AND_READS:
print("Connected.")
self._set_socket(s)
return s
示例14: connectionMade
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [as 别名]
def connectionMade(self):
log.msg('Server connected')
self.factory.di.to_server.get().addCallback(self.dataEnqueued)
try:
self.transport.getHandle().setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self.transport.getHandle().setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 30)
self.transport.getHandle().setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 1)
self.transport.getHandle().setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 5)
except:
pass
示例15: get_connection
# 需要导入模块: import socket [as 别名]
# 或者: from socket import TCP_KEEPINTVL [as 别名]
def get_connection(self, database):
"""
gets a connection to the a psql database
Args:
self.password -- the password to the database
Returns:
a connection object
"""
# additional logging to help with connection issues
boto_config_dict = self.get_boto_config()
self.log_stream.write_msg('boto', extra_msg=boto_config_dict)
log_template = "getting connection with host {0} port {1} user {2} db {3}"
log_msg = log_template.format(self.host, self.port, self.user, database)
self.log_stream.write_msg('starting', extra_msg=log_msg)
conn = psycopg2.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
database=database,
sslmode='require')
self.log_stream.write_msg('finished', extra_msg=log_msg)
fd = conn.fileno()
sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE,
self.SECONDS_BEFORE_SENDING_PROBE)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL,
self.SECONDS_BETWEEN_SENDING_PROBE)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT,
self.RETRIES_BEFORE_QUIT)
return conn