Created
April 24, 2018 17:27
-
-
Save azrakreborn/6c26f174457ad0c78d4245b91bec80db to your computer and use it in GitHub Desktop.
FTP client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Team SwagLords | |
# Christian Williams | |
# Kendrick Hillian | |
# CSC 311 | |
# 09/09/2016 | |
# FTP Client | |
import socket, getpass, zlib, gzip, os, glob | |
class FTPClient: | |
# Class variables | |
directory = 'C:\\Downloads\\' | |
server = socket.gethostname() | |
port = 8075 | |
binary_bool = True | |
ascii_bool = False | |
compression_bool = False | |
encryption_bool = False | |
debug_bool = False | |
get_bool = False | |
# Multiple files being sent | |
multi_file_bool = False | |
file_list = [] | |
def __init__(self): | |
self.login() | |
self.main() | |
def login(self): | |
while True: | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((self.server,self.port)) | |
print 'Username: anon' | |
user_input = str(getpass.getpass('Enter the password:\n>> ')) | |
while user_input == '': | |
user_input = str(getpass.getpass('Enter the password:\n>> ')) | |
sock.sendall('pass ' + user_input.lower()) | |
response = sock.recv(1024) | |
# Correct password | |
if response == 'connected': | |
print 'Connected.' | |
sock.close() | |
break | |
# Incorrect input reset state of the client | |
elif response == 'inv_c': | |
self.binary_bool = True | |
self.ascii_bool = False | |
self.compression_bool = False | |
self.encryption_bool = False | |
self.multi_file_bool = False | |
self.debug_bool = False | |
self.get = False | |
print 'Error. Re-enter the password.' | |
sock.close() | |
def main(self): | |
while True: | |
# Multiple files are being sent | |
if self.multi_file_bool: | |
if self.get_bool: | |
for file_name in self.file_list: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((self.server, self.port)) | |
s.sendall('get ' + file_name) | |
self.receive_file(s, file_name) | |
s.close() | |
self.multi_file_bool = False | |
self.get_bool = False | |
else: | |
for file_name in self.file_list: | |
#print self.file_list | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((self.server, self.port)) | |
if os.path.isfile(self.directory + str(file_name)): | |
s.sendall('put '+ str(file_name)) | |
response = s.recv(1024) | |
if response == 'received': | |
self.put(s, self.directory + str(file_name)) | |
s.close() | |
## response = s.recv(1024) | |
## if response == 'received': | |
## print file_name + ' placed on the server.' | |
elif os.path.isfile(str(file_name)): | |
#Send command to server | |
print file_name | |
s.sendall('put '+ str(file_name)) | |
if response == 'received': | |
self.put(s, self.directory + str(file_name)) | |
s.close() | |
## response = s.recv(1024) | |
## if response == 'received': | |
## print file_name + ' placed on the server.' | |
## else: | |
## print 'Error file not found.' | |
s.close() | |
self.multi_file_bool = False | |
else: | |
user_input = '' | |
# Create Socket | |
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s1.connect((self.server,self.port)) | |
# Prompt user for command | |
user_input = str(raw_input('Enter a command:\n>> ')).strip() | |
input_array = user_input.lower().split() | |
if len(input_array) == 1: | |
if input_array[0] == 'ls': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
data = s1.recv(1024) | |
print data | |
elif input_array[0] == 'binary': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.ascii_bool = False | |
self.binary_bool = True | |
data = s1.recv(1024) | |
if data == 'binary': | |
print 'Binary mode activated.' | |
elif input_array[0] == 'ascii': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.binary_bool = False | |
self.ascii_bool = True | |
data = s1.recv(1024) | |
if data == 'ascii': | |
print 'Ascii mode activated.' | |
elif input_array[0] == 'compression': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.compression_bool = True | |
data = s1.recv(1024) | |
if data == 'compression': | |
print 'Compression = On' | |
elif input_array[0] == 'encryption': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.encryption_bool = True | |
data = s1.recv(1024) | |
if data == 'encryption': | |
print 'Encryption = On' | |
elif input_array[0] == 'normal': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.compression_bool = False | |
self.encryption_bool = False | |
data = s1.recv(1024) | |
if data == 'normal': | |
print 'Compression = Off and Encryption = Off' | |
elif input_array[0] == 'pwd': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
output = s1.recv(1024) | |
print output | |
elif input_array[0] == 'quit': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
break | |
elif input_array[0] == 'quitserver': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
break | |
elif len(input_array) == 2: | |
if input_array[0] == 'mget': | |
s1.sendall(user_input.lower()) | |
self.file_list = s1.recv(1024).split('\n') | |
self.multi_file_bool = True | |
self.get_bool = True | |
elif input_array[0] == 'get': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.receive_file(s1, input_array[1]) | |
elif input_array[0] == 'ls': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
data = s1.recv(1024) | |
print data | |
elif input_array[0] == 'cd': | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
response = s1.recv(1024) | |
if response == 'inv_c': | |
print 'Directory doesn\'t exist.' | |
else: | |
print 'Directory changed to: ' + response | |
elif input_array[0] == 'mput': | |
self.multi_file_bool = True | |
self.file_list = self.list_files(self.directory + input_array[1]).split('\n') | |
elif input_array[0] == 'put': | |
if os.path.isfile(self.directory + input_array[1]): | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.put(self.directory + input_array[1]) | |
s1.close() | |
#s1.shutdown(socket.SHUT_WR) | |
## response = s1.recv(1024) | |
## if response == 'received': | |
## print input_array[1] + ' placed on the server.' | |
elif os.path.isfile(input_array[1]): | |
#Send command to server | |
s1.sendall(user_input.lower()) | |
self.put(input_array[1]) | |
s1.close() | |
#s1.shutdown(socket.SHUT_WR) | |
## response = s1.recv(1024) | |
## if response == 'received': | |
## print input_array[1] + ' placed on the server.' | |
else: | |
print 'Error file not found.' | |
elif len(input_array) > 2: | |
if input_array[0] == 'mget': | |
self.file_list = input_array[1:] | |
self.multi_file_bool = True | |
self.get_bool = True | |
elif input_array[0] == 'mput': | |
self.file_list = input_array[1:] | |
self.multi_file_bool = True | |
s1.close() | |
s1.close() | |
print 'Connection closed.' | |
def list_files(self, dir_path): | |
output = '' | |
file_list = glob.glob(dir_path) | |
for file_name in file_list: | |
#print file_list | |
if os.path.isfile(file_name): | |
path_list = file_name.split('\\') | |
output += path_list[len(path_list) - 1].strip() + '\n' | |
return output[:-1] | |
def receive_file(self, conn, file_name): | |
data = conn.recv(1024) | |
if data == 'inv_f': | |
print 'File not found.' | |
else: | |
if self.binary_bool: | |
if self.compression_bool: | |
if not self.encryption_bool: | |
# | |
if file_name[0:2].lower() == 'c:\\': | |
file_out = open(file_name + '.gz', 'wb') | |
else: | |
file_out = open(self.directory + file_name + '.gz', 'wb') | |
while True: | |
if data == '': | |
file_out.close() | |
if file_name[0:2].lower() == 'c:\\': | |
number = os.path.getsize(file_name + '.gz') | |
else: | |
number = os.path.getsize(self.directory + file_name + '.gz') | |
print number | |
if file_name[0:2].lower() == 'c:\\': | |
file_out = gzip.open(file_name + '.gz', 'rb') | |
else: | |
file_out = gzip.open(self.directory + file_name + '.gz', 'rb') | |
output = file_out.read() | |
file_out.close() | |
print 'File received.' | |
if file_name[0:2].lower() == 'c:\\': | |
file_1 = open(file_name, 'wb') | |
else: | |
file_1 = open(self.directory + file_name, 'wb') | |
file_1.write(output) | |
file_1.close() | |
if file_name[0:2].lower() == 'c:\\': | |
number = os.path.getsize(file_name ) | |
else: | |
number = os.path.getsize(self.directory + file_name) | |
if file_name[0:2].lower() == 'c:\\': | |
os.remove(file_name + '.gz') | |
else: | |
os.remove(self.directory + file_name + '.gz') | |
break | |
else: | |
file_out.write(data) | |
data = conn.recv(1024) | |
else: | |
# No Encryption | |
if not self.encryption_bool: | |
if file_name[0].lower() == 'c:\\': | |
file_out = open(file_name, 'wb') | |
else: | |
file_out = open(self.directory + file_name, 'wb') | |
while True: | |
if data == '': | |
file_out.close() | |
print 'File received.' | |
break | |
else: | |
file_out.write(data) | |
data = conn.recv(1024) | |
elif self.ascii_bool: | |
if self.compression_bool: | |
if not self.encryption_bool: | |
if file_name[0].lower() == 'c:\\': | |
file_out = open(file_name + '.gz', 'w') | |
else: | |
file_out = open(self.directory + file_name + '.gz', 'w') | |
while True: | |
if data == '': | |
file_out.close() | |
if file_name[0].lower() == 'c:\\': | |
number = os.path.getsize(file_name + '.gz') | |
else: | |
number = os.path.getsize(self.directory + file_name + '.gz') | |
print number | |
if file_name[0].lower() == 'c:\\': | |
file_out = gzip.open(file_name + '.gz', 'r') | |
else: | |
file_out = gzip.open(self.directory + file_name + '.gz', 'r') | |
output = file_out.read() | |
file_out.close() | |
print 'File received.' | |
if file_name[0].lower() == 'c:\\': | |
file_1 = open(file_name, 'w') | |
else: | |
file_1 = open(self.directory + file_name, 'w') | |
file_1.write(output) | |
file_1.close() | |
if file_name[0].lower() == 'c:\\': | |
number = os.path.getsize(file_name ) | |
else: | |
number = os.path.getsize(self.directory + file_name) | |
if file_name[0].lower() == 'c:\\': | |
os.remove(file_name + '.gz') | |
else: | |
os.remove(self.directory + file_name + '.gz') | |
break | |
else: | |
file_out.write(data) | |
data = conn.recv(1024) | |
else: | |
# No compression. | |
if not self.encryption_bool: | |
if file_name[0].lower() == 'c:\\': | |
file_out = open(file_name, 'w') | |
else: | |
file_out = open(self.directory + file_name, 'w') | |
while True: | |
if data == '': | |
file_out.close() | |
print 'File received.' | |
break | |
else: | |
file_out.write(data) | |
data = conn.recv(1024) | |
def put(self, conn, file_name): | |
if os.path.isfile(self.directory + file_name): | |
try: | |
file_in = open(self.directory + file_name, 'rb') | |
packet = file_in.read(1024) | |
while True: | |
if packet == '': | |
break | |
conn.sendall(packet) | |
packet = file_in.read(1024) | |
print 'File sent.' | |
file_in.close() | |
except IOError: | |
print 'File not found.' | |
elif os.path.isfile(file_name): | |
try: | |
file_in = open(file_name, 'rb') | |
packet = file_in.read(1024) | |
while True: | |
if packet == '': | |
break | |
conn.sendall(packet) | |
packet = file_in.read(1024) | |
print 'File sent.' | |
file_in.close() | |
except IOError: | |
print 'File not found.' | |
else: | |
print 'File not found.' | |
if __name__ == '__main__': | |
client = FTPClient() | |
del client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Team SwagLords | |
# Christian Williams | |
# Kendrick Hillian | |
# CSC 311 | |
# 09/09/2016 | |
# FTP Server | |
import socket, os, gzip, glob | |
class FTPServer: | |
# Class variables | |
server_online = True | |
binary_bool = True | |
ascii_bool = False | |
encryption_bool = False | |
compression_bool = False | |
logged_in = False | |
debug_bool = True | |
passwords = ['cw1521@jagmail.southalabama.edu', | |
'kjh1422@jagmail.southalabama.edu', | |
'test'] | |
# Replace root path with the root directory of the FTP folder | |
# Name the root FTP folder FTP | |
root_path = 'C:\\Users\\azrak\\Desktop\\FTP\\' | |
dir_path = root_path | |
def __init__(self, port): | |
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.server.bind((socket.gethostname(), port)) | |
# Listen for incoming connections | |
self.server.listen(5) | |
print 'Server created.' | |
self.main() | |
def cd(self, directory): | |
path = directory[0:4] | |
print path | |
if directory == 'root': | |
self.dir_path = self.root_path | |
return True | |
# If the directory string points to the root ftp folder, current dir path = root directory | |
elif directory.lower() == 'ftp' or directory.lower() == 'ftp\\' or directory.lower() == '\\ftp\\': | |
self.dir_path = self.root_path | |
return True | |
elif path.lower() == 'ftp\\': | |
if os.path.isdir(self.root_path + directory[4:]): | |
if directory[-1] == '\\': | |
self.dir_path = self.root_path + directory[4:] | |
else: | |
self.dir_path = self.root_path + directory[4:] + '\\' | |
return True | |
else: | |
return False | |
elif path.lower() == '\\ftp': | |
if os.path.isdir(self.root_path + directory[5:]): | |
if directory[-1] == '\\': | |
self.dir_path = self.root_path + directory[5:] | |
else: | |
self.dir_path = self.root_path + directory[5:] + '\\' | |
return True | |
else: | |
return False | |
elif os.path.isdir(self.dir_path + directory): | |
if directory[-1] == '\\': | |
self.dir_path = self.dir_path + directory | |
else: | |
self.dir_path = self.dir_path + directory + '\\' | |
return True | |
elif os.path.isdir(self.dir_path + directory[1:]): | |
if directory[-1] == '\\': | |
self.dir_path = self.dir_path + directory[1:] | |
else: | |
self.dir_path = self.dir_path + directory[1:] + '\\' | |
return True | |
else: | |
return False | |
def send_file(self, file_name): | |
if file_name[0:3].lower() != 'c:\\': | |
file_name = self.dir_path + file_name.strip() | |
print file_name.lower() | |
if self.debug_bool: | |
if self.binary_bool: | |
print 'binary' | |
if self.compression_bool: | |
print 'compression' | |
# binary mode on, compression on, encryption off | |
if not self.encryption_bool: | |
print 'no encryption' | |
# try to open the file | |
try: | |
# read and close the file | |
file_in = open(file_name, 'rb') | |
file_string = file_in.read() | |
file_in.close() | |
# Open a gzip object and write the file to it | |
compressed_file = gzip.open(file_name + '.gz', 'wb') | |
compressed_file.write(file_string) | |
compressed_file.close() | |
#print os.path.getsize(file_name + '.gz') | |
compressed_file = open(file_name + '.gz', 'rb') | |
packet = compressed_file.read(1024) | |
while packet: | |
self.conn.sendall(packet) | |
packet = compressed_file.read(1024) | |
print('File sent.') | |
compressed_file.close() | |
os.remove(file_name + '.gz') | |
except IOError: | |
print('File not found.') | |
else: | |
# compression off | |
try: | |
file_in = open(file_name, 'rb') | |
packet = file_in.read(1024) | |
while packet: | |
self.conn.sendall(packet) | |
packet = file_in.read(1024) | |
print('File sent.') | |
file_in.close() | |
except IOError: | |
print('File not found.') | |
# ascii mode on | |
elif self.ascii_bool: | |
#print 'ascii' | |
if self.compression_bool: | |
#print 'compression' | |
if not self.encryption_bool: | |
#print 'no encryption' | |
try: | |
file_in = open(file_name, 'r') | |
file_string = file_in.read() | |
file_in.close() | |
compressed_file = gzip.open(file_name + '.gz', 'wb') | |
compressed_file.write(file_string) | |
compressed_file.close() | |
print os.path.getsize(file_name + '.gz') | |
compressed_file = open(file_name + '.gz', 'rb') | |
file_in = compressed_file.readlines() | |
compressed_file.close() | |
for i in range(len(file_in)): | |
self.conn.sendall(file_in[i]) | |
print('File sent.') | |
os.remove(file_name + '.gz') | |
except IOError: | |
print('File not found.') | |
else: | |
file_in = open(file_name, 'r') | |
file_array = file_in.readlines() | |
file_in.close() | |
for i in range(len(file_array)): | |
self.conn.sendall(file_array[i]) | |
print('File sent.') | |
def recv_file(self, file_name): | |
name_array = file_name.split('\\') | |
name = name_array[len(name_array) - 1] | |
if name[-3:] == 'v_c': | |
name = name[:-5] | |
try: | |
file_in = open(self.dir_path + name, 'wb') | |
print 'file opened' | |
while True: | |
packet = self.conn.recv(1024) | |
if packet == '': | |
break | |
file_in.write(packet) | |
print 'File received.' | |
file_in.close() | |
except IOError: | |
print 'File not found.' | |
def options(self): | |
menu_options = 'Menu:\nget\tquit\nls' | |
return menu_options | |
def ls(self, dir_path): | |
output = '' | |
file_list = glob.glob(dir_path) | |
for file_name in file_list: | |
#print file_list | |
if os.path.isfile(file_name): | |
path_list = file_name.split('\\') | |
output += path_list[len(path_list) - 1].strip() + '\n' | |
return output[:-1] | |
def check_pass(self, password): | |
if password.lower() in self.passwords: | |
print 'Correct password.' | |
return True | |
else: | |
print 'Incorrect password.' | |
return False | |
def menu(self): | |
print 'main entered' | |
try: | |
while True: | |
user_input = self.conn.recv(1024) | |
if not self.logged_in: | |
print 'checking password' | |
self.logged_in = self.check_pass(user_input.split()[1]) | |
if self.logged_in: | |
self.conn.sendall('connected') | |
else: | |
self.conn.sendall('failed') | |
break | |
else: | |
command = user_input.split() | |
print 'input split' | |
print command | |
if len(command) == 1: | |
if command[0] == 'ls': | |
self.conn.sendall(self.ls(self.dir_path + '*')) | |
break | |
elif command[0] == 'binary': | |
self.ascii_bool = False | |
self.binary_bool = True | |
self.conn.sendall('binary') | |
break | |
elif command[0] == 'ascii': | |
self.binary_bool = False | |
self.ascii_bool = True | |
self.conn.sendall('ascii') | |
break | |
elif command[0] == 'compression': | |
self.compression_bool = True | |
self.conn.sendall('compression') | |
break | |
elif command[0] == 'encryption': | |
self.encryption_bool = True | |
self.conn.sendall('encryption') | |
break | |
elif command[0] == 'normal': | |
self.compression_bool = False | |
self.encryption_bool = False | |
self.conn.sendall('normal') | |
break | |
elif command[0] == 'pwd': | |
self.conn.sendall(self.dir_path) | |
break | |
elif command[0] == 'help': | |
self.conn.sendall(self.options()) | |
break | |
elif command[0] == 'quitserver': | |
self.server_online = False | |
self.logged_in = False | |
break | |
elif command[0] == 'quit': | |
self.logged_in = False | |
break | |
else: | |
print 'Invalid command.' | |
break | |
# Commands containing 2 elements | |
elif len(command) == 2: | |
if command[0] == 'ls': | |
self.conn.sendall(self.ls(self.dir_path + command[1])) | |
break | |
elif command[0] == 'mget': | |
self.conn.sendall(self.ls(self.dir_path + command[1])) | |
break | |
elif command[0] == 'get': | |
if os.path.isfile(self.dir_path + command[1]): | |
self.send_file(self.dir_path + command[1]) | |
break | |
else: | |
self.conn.sendall('inv_f') | |
elif command[0] == 'pass': | |
self.logged_in = False | |
self.binary_bool = True | |
self.ascii_bool = False | |
self.encryption_bool = False | |
self.compression_bool = False | |
self.dir_path = self.root_path | |
self.conn.sendall('inv_c') | |
break | |
elif command[0] == 'cd': | |
dir_changed_bool = self.cd(command[1]) | |
if dir_changed_bool: | |
self.conn.sendall(self.dir_path) | |
print 'Successfully changed.' | |
else: | |
self.conn.sendall('inv_c') | |
print 'Directory doesnt exist.' | |
break | |
elif command[0] == 'put': | |
self.conn.sendall('received') | |
self.conn.shutdown(socket.SHUT_WR) | |
self.recv_file(command[1]) | |
self.conn.close() | |
break | |
else: | |
print 'Invalid command.' | |
self.conn.sendall('inv_c') | |
break | |
else: | |
break | |
self.conn.close() | |
print 'Connection closed.' | |
except socket.error, msg: | |
self.conn.close() | |
print "Error: %s" % msg | |
def main(self): | |
while True: | |
# Accept incoming connection | |
self.conn, self.address = self.server.accept() | |
print('Connection from: ' + str(self.address)) | |
self.menu() | |
if self.server_online == False: | |
break | |
self.server.close() | |
if __name__ == '__main__': | |
ftp = FTPServer(8075) | |
del ftp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment