-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyftp.py
More file actions
90 lines (66 loc) · 2.3 KB
/
pyftp.py
File metadata and controls
90 lines (66 loc) · 2.3 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
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
83
84
85
86
87
88
89
90
# A simple python ftp module
# author: maxwellmandela
import ftplib
import os
import config
def make_connection_uri():
uri = raw_input('Enter connection the URI(server:user@password): ')
host = uri.split(':')[0]
username = uri.split(':')[1].split('@')[0]
password = uri.split(':')[1].split('@')[1]
print "Connecting.."
try:
ftp = ftplib.FTP(host, username, password)
print "Connection established"
return ftp
except Exception, err:
print "Connection error %s:" % (str(err))
return False
def make_connection():
host = raw_input('Hostname: ')
user = raw_input('Username: ')
password = raw_input('Password: ')
try:
ftp = ftplib.FTP(host or config.host, user or config.user, password or config.password)
return ftp
except Exception, err:
print "Connection error %s:" % (str(err))
return False
def connection_method():
connection_option = input('Pick a way to connect(1. URI, 2. Host, Password, User): ')
if connection_option == 1:
return make_connection_uri()
return make_connection()
def get_files(con, directory):
contents = con.nlst(directory)
for item in contents:
print "Found file %s:" % item
def upload(con, file_path):
file_path = os.path.join(file_path)
if os.path.isfile(file_path):
print "File exists, attempting upload.."
ext = os.path.splitext(file_path)[1]
if ext in (".txt", ".htm", ".html"):
new_path = con.storlines("STOR " + file_path, open(file_path))
else:
new_path = con.storbinary("STOR " + file_path, open(file_path, "rb"), 1024)
return new_path
else:
return "File not exists, aborting.."
def run():
con = connection_method()
if con:
directory = raw_input('Enter upload directory/path: ')
directory = directory or config.directory
file_path = raw_input('Enter file path: ')
print "Files in %s:" % directory
get_files(con, directory)
try:
con.cwd(directory)
upload(con, file_path)
print "File uploaded to", con.pwd()
except Exception, e:
print "File not uploaded to server %s" % (str(e))
else:
print "Server connection not established, aborting.."
run()