def list_files(): global __serverIP # Establishes a data connection link = DataLink(__serverIP) # Asks for the files list send_request("LIST", startsWith="150") log("Receiving list of files...") # Receives the list rawData = link.receive_data().decode("ascii") files = __format_files_list(rawData) log("Found " + str(len(files)) + " files") # Closes the data connection link.close() return files
def send_file(path, destName=None): global __serverIP if not destName: destName = __extract_name(path) # Establishes a data connection link = DataLink(__serverIP) # Asks for a file upload send_request("STOR " + destName) # Sends the file data datafile = open(path, "rb") filedata = datafile.read() log("Sending data from " + path + "...") link.send_data(filedata) datafile.close() # Closes the data connection link.close()
def fetch_file(name, destPath=None): global __serverIP if not destPath: destPath = name # Establishes a data connection link = DataLink(__serverIP) # Asks for a file download send_request("RETR " + name, startsWith="150") log("Receiving data from " + name + "...") # Receives the file filedata = link.receive_data() datafile = open(destPath, "wb+") datafile.write(filedata) datafile.close() # Closes the data connection link.close()
def __init__(self, ip): global _connection_timeout global _transmission_timeout # Establishes a data connection pasvResp = send_request("PASV", startsWith="227") dataPort = _decode_pasv(pasvResp) log("Connecting data channel at port " + str(dataPort) + "...") self.socket = socket.socket() self.socket.settimeout(_connection_timeout) self.socket.connect((ip, dataPort)) self.socket.settimeout(_transmission_timeout) # Sets up a callback to know when the transfer is done self.transfer_complete = False def action(line): nonlocal self self.transfer_complete = True filter = lambda line: line.startswith("226") commandlink.add_callback(action, filter)
def connect(hostname, user, password): global __serverIP global __hostName global __timeout global __username global __password global __storageDir # Finds the ip address log("Finding ip of host " + hostname + "...") try: ip = socket.gethostbyname(hostname) except: log("Could not resolve host " + hostname, "E: ") raise Exception("Could not resolve host " + hostname) __serverIP = ip # Opens the command link and sets the data link up commandlink.open(ip) # Sends the authentication messages send_request("AUTH SSL", startsWith="530") send_request("USER " + user, startsWith="331") send_request("PASS " + password, startsWith="230") # Sets the connection up send_request("TYPE I", startsWith="200") __change_directory(__storageDir)
def __change_directory(dir): send_request("CWD " + dir, startsWith="250")
def backup_file(name): global __backupDir global __storageDir send_request("RNFR " + __storageDir + "/" + name) send_request("RNTO " + __backupDir + "/" + name)