Skip to content

Commit

Permalink
Added ability to log in noninteractively
Browse files Browse the repository at this point in the history
- user can now provide the host, username, and password when
  executing the script to speed up logging in
 Changes to be committed:
	modified:   ftpy
  • Loading branch information
Muhammad-Sharif Moustafa committed Aug 14, 2015
1 parent 6cb7407 commit dc0bd39
Showing 1 changed file with 71 additions and 31 deletions.
102 changes: 71 additions & 31 deletions ftpy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from ftplib import error_temp
from ftplib import error_perm
from getpass import getpass
from sys import exit
import sys
import socket
import os

Expand All @@ -15,35 +16,17 @@ host = ''
user = ''
password = ''
msg = ''
connected = False
loggedIn = False
file = None

# Convenience Functions
def writeFileLineByLine(line):
file.write(line)
file.write('\n')

def writeBinary(data):
file.write(data)

def getUserInput(msg):
inputString = ''
# Try the Python 2 way of getting input. If that fails
# use the Python 3 way.
try:
inputString = eval("raw_input('" + msg + "')")
except (SyntaxError, NameError):
inputString = eval("input('" + msg + "')")
return inputString

# Connection and Login Logic
connected = False
while not connected:
host = getUserInput('Host name (URL): ').strip()
if (host == 'quit' or host == 'exit'):
def connect(_host):
if (_host == 'quit' or _host == 'exit'):
exit(0)
if (host != ''):
if (_host != ''):
try:
msg = ftp.connect(host)
msg = ftp.connect(_host)
except error_temp as e:
print(e)
except error_perm as e:
Expand All @@ -55,21 +38,78 @@ while not connected:
except socket.error as e:
print("Socket error: " + str(e))
else:
connected = True
print(msg)
loggedIn = False
while not loggedIn:
user = getUserInput('Username (blank for anonymous): ').strip()
password = getpass('Password (blank for anonymous): ').strip()
return True
return False

def login(_user, _password):
try:
msg = ftp.login(user, password)
msg = ftp.login(_user, _password)
except error_temp as e:
print(e)
except error_perm as e:
print(e)
else:
loggedIn = True
print(msg)
return True
return False

def writeFileLineByLine(line):
file.write(line)
file.write('\n')

def writeBinary(data):
file.write(data)

def getUserInput(msg):
inputString = ''
# Try the Python 2 way of getting input. If that fails
# use the Python 3 way.
try:
inputString = eval("raw_input('" + msg + "')")
except (SyntaxError, NameError):
inputString = eval("input('" + msg + "')")
return inputString

# Parse command line arguments (if any) and try to connect
# with credentials in command line
if len(sys.argv) == 2:
host = sys.argv[1]
connected = connect(host)

if len(sys.argv) == 3:
# user provided host and user
host = sys.argv[1]
user = sys.argv[2]
# try to connect
connected = connect(host)

if len(sys.argv) == 4:
# user provided host and user
host = sys.argv[1]
user = sys.argv[2]
password = sys.argv[3]
# try to connect
connected = connect(host)
# try to login
if connected:
loggedIn = login(user, password)

# Connection and Login Logic
while not connected:
host = getUserInput('Host name (URL): ').strip()
connected = connect(host)

while not loggedIn:
if user == '':
user = getUserInput('Username (blank for anonymous): ').strip()
if password == '':
password = getpass('Password (blank for anonymous): ').strip()
loggedIn = login(user, password)
# if login attempt was unsuccessful, clear username and password
if not loggedIn:
user = ''
password = ''

# Command Loop
while True:
Expand Down

0 comments on commit dc0bd39

Please sign in to comment.