-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsftp_utility.py
More file actions
28 lines (23 loc) · 974 Bytes
/
sftp_utility.py
File metadata and controls
28 lines (23 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import paramiko as paramiko
from config import Config
class SftpUtility:
def __init__(self):
self.ssh_client = paramiko.SSHClient()
self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh_client.connect(hostname=Config.SFTP_HOST,
port=int(Config.SFTP_PORT),
username=Config.SFTP_USERNAME,
password=Config.SFTP_PASSWORD,
look_for_keys=False,
timeout=120)
def __enter__(self):
self._sftp_client = self.ssh_client.open_sftp()
return self
def __exit__(self, *_):
self._sftp_client.close()
self.ssh_client.close()
def write_file_to_sftp(self, file_name, data):
file_path = Config.SFTP_DIR + '/' + file_name
file = self._sftp_client.open(file_path, "a", -1)
file.write(data)
file.flush()