-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_client.py
48 lines (38 loc) · 1.57 KB
/
ftp_client.py
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
from ftplib import FTP
import os
import argparse
import urlparse
class ftp_client():
def __init__(self, server, account, pwd):
self.ftp = FTP(server)
self.ftp.set_pasv(True)
print 'login...'
self.ftp.login(account, pwd)
def chdir(self, dir):
try:
self.ftp.mkd(dir)
except:
pass
self.ftp.cwd(dir)
# Check if directory exists (in current location)
def directory_exists(self, dir):
if dir in self.ftp.nlst():
return True
return False
def upload_file(self, remote_folder, local_fpath):
print 'chdir...'
self.chdir(remote_folder)
fname = os.path.basename(local_fpath)
print 'upload...'
self.ftp.storbinary("STOR " + fname, open(local_fpath, 'rb'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", help="ftp ip address", type=str, required=True)
parser.add_argument("-a", "--account", help="account", type=str, required=True)
parser.add_argument("-p", "--pwd", help="password", type=str, required=True)
parser.add_argument("-f", "--localfile", help="local file path", type=str, required=False)
parser.add_argument("-rd", "--remotedir", help="download file name", type=str, required=False, default='/')
args = parser.parse_args()
print args
f = ftp_client(server = args.server, account = args.account, pwd = args.pwd)
f.upload_file(remote_folder = args.remotedir, local_fpath = args.localfile)