-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsftpclient.py
More file actions
49 lines (37 loc) · 1.38 KB
/
sftpclient.py
File metadata and controls
49 lines (37 loc) · 1.38 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
"""
Wrapper class for SFTP connections
"""
from paramiko import ssh_exception, AutoAddPolicy, SSHClient
class SFTPClient():
"""Wrapper around third-party SFTP client"""
def __init__(self, host, username, password, port: int = 22):
"""Set up SFTP connection"""
paramiko = SSHClient()
paramiko.set_missing_host_key_policy(AutoAddPolicy)
try:
paramiko.connect(hostname=host, username=username, password=password, port=port)
except ssh_exception.AuthenticationException as error:
raise SFTPAuthError(error)
self.sftp = paramiko.open_sftp()
def cwd(self, path):
"""Change working directory"""
self.sftp.chdir(path=path)
def mkdir(self, path):
"""Make new directory"""
self.sftp.mkdir(path=path)
def remove(self, path):
"""Remove specified file"""
self.sftp.remove(path=path)
def putfo(self, flo, remotepath):
"""Copy file-like object to server"""
self.sftp.putfo(fl=flo, remotepath=remotepath)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
"""Safely close SFTP connection"""
self.sftp.close()
class SFTPAuthError(Exception):
"""Exception to report SFTP authentication errors"""
def __init__(self, message):
super().__init__(message)