使用秘钥文件登录sftp
def __init__(self):
# logging.basicConfig(level=logging.INFO, format="%(filename)s [line:%(lineno)d] %(message)s")
self.logger = logging.getLogger(__name__)
self.logger.setLevel(level=logging.INFO)
# 获取settings配置数据
self.settings = get_project_settings()
self.host = self.settings['FTP_HOST']
self.port = self.settings['FTP_PORT']
self.username = self.settings['FTP_USERNAME']
self.pwd = self.settings['FTP_PASSWORD']
# self.transport = paramiko.Transport((self.host, self.port))
# self.transport.connect(username=self.username, password=self.pwd)
self.screenshot_path = self.settings['SCREENSHOT_PATH']
self.target_path = self.settings['FTP_TARGET_PATH']
# 使用秘钥登录sftp
private_key = paramiko.RSAKey.from_private_key_file('id_rsa')
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=self.host, port=self.port, username=self.username, pkey=private_key)
self.transport = ssh_client.get_transport()