本文整理汇总了Python中sshtunnel.SSHTunnelForwarder.set_keepalive方法的典型用法代码示例。如果您正苦于以下问题:Python SSHTunnelForwarder.set_keepalive方法的具体用法?Python SSHTunnelForwarder.set_keepalive怎么用?Python SSHTunnelForwarder.set_keepalive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sshtunnel.SSHTunnelForwarder
的用法示例。
在下文中一共展示了SSHTunnelForwarder.set_keepalive方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DatabaseReachPasswordTimeout
# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import set_keepalive [as 别名]
def DatabaseReachPasswordTimeout(self,p_database_index):
if not self.v_databases[p_database_index]['prompt_password']:
return { 'timeout': False, 'message': ''}
else:
#Create tunnel if enabled
if self.v_databases[p_database_index]['tunnel']['enabled']:
v_create_tunnel = False
if self.v_databases[p_database_index]['tunnel_object'] != None:
try:
result = 0
v_tunnel_object = tunnels[self.v_databases[p_database_index]['database'].v_conn_id]
if not v_tunnel_object.is_active:
v_tunnel_object.stop()
v_create_tunnel = True
except Exception as exc:
v_create_tunnel = True
None
if self.v_databases[p_database_index]['tunnel_object'] == None or v_create_tunnel:
try:
if self.v_databases[p_database_index]['tunnel']['key'].strip() != '':
v_file_name = '{0}'.format(str(time.time())).replace('.','_')
v_full_file_name = os.path.join(settings.TEMP_DIR, v_file_name)
with open(v_full_file_name,'w') as f:
f.write(self.v_databases[p_database_index]['tunnel']['key'])
server = SSHTunnelForwarder(
(self.v_databases[p_database_index]['tunnel']['server'], int(self.v_databases[p_database_index]['tunnel']['port'])),
ssh_username=self.v_databases[p_database_index]['tunnel']['user'],
ssh_private_key_password=self.v_databases[p_database_index]['tunnel']['password'],
ssh_pkey = v_full_file_name,
remote_bind_address=(self.v_databases[p_database_index]['database'].v_active_server, int(self.v_databases[p_database_index]['database'].v_active_port)),
logger=logger
)
else:
server = SSHTunnelForwarder(
(self.v_databases[p_database_index]['tunnel']['server'], int(self.v_databases[p_database_index]['tunnel']['port'])),
ssh_username=self.v_databases[p_database_index]['tunnel']['user'],
ssh_password=self.v_databases[p_database_index]['tunnel']['password'],
remote_bind_address=(self.v_databases[p_database_index]['database'].v_active_server, int(self.v_databases[p_database_index]['database'].v_active_port)),
logger=logger
)
server.set_keepalive = 120
server.start()
s = SessionStore(session_key=self.v_user_key)
tunnels[self.v_databases[p_database_index]['database'].v_conn_id] = server
self.v_databases[p_database_index]['tunnel_object'] = str(server.local_bind_port)
self.v_databases[p_database_index]['database'].v_connection.v_host = '127.0.0.1'
self.v_databases[p_database_index]['database'].v_connection.v_port = server.local_bind_port
#GO OVER ALL TABS CONNECTION OBJECTS AND UPDATE HOST AND PORT FOR THIS CONN_ID
try:
for k in list(self.v_tab_connections.keys()):
if self.v_tab_connections[k].v_conn_id == p_database_index:
self.v_tab_connections[k].v_connection.v_host = '127.0.0.1'
self.v_tab_connections[k].v_connection.v_port = server.local_bind_port
except Exception:
None
s['omnidb_session'] = self
s.save()
except Exception as exc:
return { 'timeout': True, 'message': str(exc)}
#Reached timeout, must request password
if not self.v_databases[p_database_index]['prompt_timeout'] or datetime.now() > self.v_databases[p_database_index]['prompt_timeout'] + timedelta(0,custom_settings.PWD_TIMEOUT_TOTAL):
#Try passwordless connection
self.v_databases[p_database_index]['database'].v_connection.v_password = ''
v_test = self.v_databases[p_database_index]['database'].TestConnection()
if v_test=='Connection successful.':
s = SessionStore(session_key=self.v_user_key)
s['omnidb_session'].v_databases[p_database_index]['prompt_timeout'] = datetime.now()
s['omnidb_session'].v_databases[p_database_index]['database'].v_connection.v_password = ''
s.save()
return { 'timeout': False, 'message': ''}
else:
return { 'timeout': True, 'message': v_test}
#Reached half way to timeout, update prompt_timeout
if datetime.now() > self.v_databases[p_database_index]['prompt_timeout'] + timedelta(0,settings.PWD_TIMEOUT_REFRESH):
s = SessionStore(session_key=self.v_user_key)
s['omnidb_session'].v_databases[p_database_index]['prompt_timeout'] = datetime.now()
s.save()
return { 'timeout': False, 'message': ''}