def get(self, remote, local=None): """ Gets the file from FTP server local can be: a file: opened for writing, left open a string: path to output file None: contents are returned """ if isinstance(local, file_type): # open file, leave open local_file = local elif local is None: # return string local_file = buffer_type() else: # path to file, open, write/close return None local_file = open(local, 'wb') self.conn.retrbinary("RETR %s" % remote, local_file.write) if isinstance(local, file_type): pass elif local is None: contents = local_file.getvalue() local_file.close() return contents else: local_file.close() return None
def put(self, local, remote, contents=None, quiet=False): """ Puts a local file (or contents) on to the FTP server local can be: a string: path to inpit file a file: opened for reading None: contents are pushed """ remote_dir = os.path.dirname(remote) remote_file = os.path.basename(local)\ if remote.endswith('/') else os.path.basename(remote) if contents: # local is ignored if contents is set local_file = buffer_type(contents) elif isinstance(local, file_type): local_file = local else: local_file = open(local, 'rb') current = self.conn.pwd() self.descend(remote_dir, force=True) size = 0 try: self.conn.storbinary('STOR %s' % remote_file, local_file) size = self.conn.size(remote_file) except: if not quiet: raise finally: local_file.close() self.conn.cwd(current) return size