Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment