This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsendfile.py
82 lines (65 loc) · 1.86 KB
/
sendfile.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env Python
import sys
import ftplib
import datetime
from ftplib import FTP
# Usage:
# sendfile.py "filename" "ftppath" "host" "ip"
def printf(string):
print(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S') + " : " + string);
def checkfolder(ftp):
files = []
try:
files = ftp.nlst()
except ftplib.error_perm, resp:
if str(resp) != "550 No files found":
printf("Couldn't retrieve file list")
else:
return
for f in files:
if (".plg" in f):# or "CTRPFData.bin" in f):
parts = f.split()
name = parts[len(parts) - 1]
printf("Deleting " + name)
try:
ftp.delete(name)
printf(name + " was successfully deleted")
except Exception:
printf("An error occured")
continue
if __name__ == '__main__':
print("");
printf("FTP File Sender\n")
try:
filename = sys.argv[1]
path = sys.argv[2]
host = sys.argv[3]
port = sys.argv[4]
ftp = FTP()
printf("Connecting to " + host + ":" + port);
ftp.connect(host, port);
printf("Connected");
printf("Opening " + filename);
file = open(sys.argv[1], "rb");
printf("Success");
except IOError as e:
printf("/!\ An error occured. /!\ ");
printf("Moving to: ftp:/" + path);
try:
ftp.cwd(path);
except IOError:
try:
ftp.mkd(path);
ftp.cwd(path);
except Exception:
pass
checkfolder(ftp)
try:
printf("Sending file");
ftp.storbinary('STOR '+ filename, file);
printf("Done")
file.close();
ftp.quit();
printf("Disconnected");
except Exception:
printf("/!\ An error occured. /!\ ");