正在重试连接Paramiko-Python



编写了一个函数,在断开连接时尝试重新连接到SSH。基本上扩展了我现有的功能,只保存了图像,效果很好。代码运行,但无法重新建立连接。如有任何帮助,我们将不胜感激。

def get_image_id_and_upload_folder_of_images(db_name, table_name, selector_list,
condition_label, condition_val, img_address):
"""
get image id using project name from db
:param db_name: str - name of the data-base (usually 'server')
:param table_name: str - name of the table (usually 'images')
:param selector_list: list of str - list of selectors for the query (usually ["id", "path"])
:param condition_label: str - condition for the sql statement (like 'path')
:param condition_val: list of str - value for the condition of the condition_label (like ['name_of_file.png'])
:param img_address:  str - address of the images to send them to the ftp server
:return: returns image or project id
"""
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
retry_interval = 1
retry_interval = float(retry_interval)
timeout = int(20)
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(retry_interval)
try:
db, cur = db_info(db_name)
cond_list = ["'" + str(x) + "'" for x in condition_val]
condition = ",".join(cond_list)
selector = ",".join(selector_list)
# make a query for sql
query_stmt = "SELECT " + selector + " FROM " + table_name + " WHERE `" + 
condition_label + "` IN (" + str(condition) + ");"
image_ids = get_image_ids(cur, db, query_stmt, condition_val)
for idx, ids in enumerate(image_ids):
print(ids)
save_img_new_server(img_address + '/' + condition_val[idx], str(ids))
save_img_new_server(img_address + '/' + condition_val[idx], str(ids), hst="site.com",
folder_path='images/')
except paramiko.ssh_exception.NoValidConnectionsError as e:
print('SSH transport is not ready...')
continue
# print(img_address + '/' + condition_val[idx], str(ids))
return image_ids

您的代码从不调用client.connect()。事实上,它在while循环中根本不与任何paramiko模块交互。

最新更新