def get_latest_event_id(self): """Query the ftp server and determine the latest event id. :return: A string containing a valid event id. :raises: NetworkError """ ftp_client = FtpClient() try: ftp_client_list = ftp_client.get_listing() ftp_client_list.sort(key=lambda x: x.lower()) except NetworkError: raise now = datetime.now() now = int( '%04d%02d%02d%02d%02d%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second)) event_id = now + 1 while int(event_id) > now: if len(ftp_client_list) < 1: raise EventIdError('Latest Event Id could not be obtained') event_id = ftp_client_list.pop().split('/')[-1].split('.')[0] if event_id is None: raise EventIdError('Latest Event Id could not be obtained') self.event_id = event_id
def get_latest_event_id(self): """Query the ftp server and determine the latest event id. :return: A string containing a valid event id. :raises: NetworkError """ ftp_client = FtpClient() try: ftp_client_list = ftp_client.get_listing() ftp_client_list.sort(key=lambda x: x.lower()) except NetworkError: raise now = datetime.now() now = int( '%04d%02d%02d%02d%02d%02d' % ( now.year, now.month, now.day, now.hour, now.minute, now.second )) event_id = now + 1 while int(event_id) > now: if len(ftp_client_list) < 1: raise EventIdError('Latest Event Id could not be obtained') event_id = ftp_client_list.pop().split('/')[-1].split('.')[0] if event_id is None: raise EventIdError('Latest Event Id could not be obtained') self.event_id = event_id
def getLatestEventId(self): """Query the ftp server and determine the latest event id. Args: None Returns: A string containing a valid event id. Raises: NetworkError """ myFtpClient = FtpClient() try: myList = myFtpClient.getListing() myList.sort(key=lambda x: x.lower()) except NetworkError: raise now = datetime.now() now = int( '%04d%02d%02d%02d%02d%02d' % ( now.year, now.month, now.day, now.hour, now.minute, now.second )) myEventId = now + 1 while int(myEventId) > now: if len(myList) < 1: raise EventIdError('Latest Event Id could not be obtained') myEventId = myList.pop().split('/')[-1].split('.')[0] if myEventId is None: raise EventIdError('Latest Event Id could not be obtained') self.eventId = myEventId
def testGetDirectoryListing(self): """Check if we can get a nice directory listing""" myClient = FtpClient() myListing = myClient.getListing() #Make it a single string myListing = '\n'.join(myListing) myMessage = ('Expected this list:\n%s\nTo contain these items:\n%s' % (myListing, self._expectedMatches)) for myExpectedFile in self._expectedMatches: assert re.search(myExpectedFile, myListing), myMessage
def testGetFile(self): """Test that the ftp client can fetch a file ok""" myClient = FtpClient() myListing = myClient.getListing() #Make it a single string myListing = '\n'.join(myListing) myMessage = ('Expected outcome:\n%s\nActual outcome:\n%s' % (myListing, self._expectedMatches)) for myExpectedFile in self._expectedMatches: assert re.search(myExpectedFile, myListing), myMessage
def is_on_server(self): """Check the event associated with this instance exists on the server. :return: True if valid, False if not :raises: NetworkError """ input_file_name, output_file_name = self.file_names() file_list = [input_file_name, output_file_name] ftp_client = FtpClient() return ftp_client.has_files(file_list)
def load_menu(self, file_name): """ Downloads menu from FTP server to local system Parameters ---------- file_name : str Menu file name """ ftpClient = FtpClient() ftpClient.download_file_from_ftp_server(file_name)
def isOnServer(self): """Check the event associated with this instance exists on the server. Args: None Returns: True if valid, False if not Raises: NetworkError """ myInpFileName, myOutFileName = self.fileNames() myList = [myInpFileName, myOutFileName] myFtpClient = FtpClient() return myFtpClient.hasFiles(myList)
def getLatestEventId(self): """Query the ftp server and determine the latest event id. Args: None Returns: A string containing a valid event id. Raises: NetworkError """ myFtpClient = FtpClient() try: myList = myFtpClient.getListing() except NetworkError: raise myEventId = myList[-1].split('/')[-1].split('.')[0] if myEventId is None: raise EventIdError('Latest Event Id could not be obtained') self.eventId = myEventId
def _fetch_file(self, event_file, retries=3): """Private helper to fetch a file from the ftp site. e.g. for event 20110413170148 this file would be fetched:: ftp://118.97.83.243/20110413170148.inp.zip and this local file created:: /tmp/realtime/20110413170148.inp.zip .. note:: If a cached copy of the file exits, the path to the cache copy will simply be returned without invoking any network requests. :param event_file: Filename on server e.g.20110413170148.inp.zip :type event_file: str :param retries: Number of reattempts that should be made in in case of network error etc. :type retries: int :return: A string for the dataset path on the local storage system. :rtype: str :raises: EventUndefinedError, NetworkError """ # Return the cache copy if it exists local_path = os.path.join(shakemap_zip_dir(), event_file) if os.path.exists(local_path): return local_path #Otherwise try to fetch it using ftp for counter in range(retries): last_error = None try: client = FtpClient() client.get_file(event_file, local_path) except NetworkError, e: last_error = e except:
def _fetchFile(self, theEventFile, theRetries=3): """Private helper to fetch a file from the ftp site. e.g. for event 20110413170148 this file would be fetched:: ftp://118.97.83.243/20110413170148.inp.zip and this local file created:: /tmp/realtime/20110413170148.inp.zip .. note:: If a cached copy of the file exits, the path to the cache copy will simply be returned without invoking any network requests. Args: * theEventFile: str - filename on server e.g.20110413170148.inp.zip * theRetries: int - number of reattempts that should be made in in case of network error etc. Returns: str: A string for the dataset path on the local storage system. Raises: EventUndefinedError, NetworkError """ # Return the cache copy if it exists myLocalPath = os.path.join(shakemapZipDir(), theEventFile) if os.path.exists(myLocalPath): return myLocalPath #Otherwise try to fetch it using ftp for myCounter in range(theRetries): myLastError = None try: myClient = FtpClient() myClient.getFile(theEventFile, myLocalPath) except NetworkError, e: myLastError = e except:
my_event_id = sys.argv[1] if my_event_id in "--list": # ftp_client = FtpClient() sftp_client = SFtpClient() # myListing = ftp_client.get_listing() dir_listing = sftp_client.get_listing(my_func=is_event_id) for event in dir_listing: print event sys.exit(0) elif my_event_id in "--run-all": # # Caution, this code path gets memory leaks, use the # batch file approach rather! # ftp_client = FtpClient() dir_listing = ftp_client.get_listing() for event in dir_listing: if "out" not in event: continue event = event.replace("ftp://118.97.83.243/", "") event = event.replace(".out.zip", "") print "Processing %s" % event # noinspection PyBroadException try: process_event(event, my_locale) except: # pylint: disable=W0702 LOGGER.exception("Failed to process %s" % event) sys.exit(0) else: process_event(my_event_id, my_locale)
myEventId = sys.argv[1] if myEventId in '--list': # myFtpClient = FtpClient() mySftpClient = SFtpClient() # myListing = myFtpClient.getListing() myListing = mySftpClient.getListing(my_func=is_event_id) for myEvent in myListing: print myEvent sys.exit(0) elif myEventId in '--run-all': # # Caution, this code path gets memory leaks, use the # batch file approach rather! # myFtpClient = FtpClient() myListing = myFtpClient.getListing() for myEvent in myListing: if 'out' not in myEvent: continue myEvent = myEvent.replace('ftp://118.97.83.243/', '') myEvent = myEvent.replace('.out.zip', '') print 'Processing %s' % myEvent try: processEvent(myEvent, myLocale) except: # pylint: disable=W0702 LOGGER.exception('Failed to process %s' % myEvent) sys.exit(0) else: processEvent(myEventId, myLocale)
myEventId = sys.argv[1] if myEventId in '--list': # myFtpClient = FtpClient() mySftpClient = SFtpClient() # myListing = myFtpClient.getListing() myListing = mySftpClient.getListing(my_func=is_event_id) for myEvent in myListing: print myEvent sys.exit(0) elif myEventId in '--run-all': # # Caution, this code path gets memory leaks, use the # batch file approach rather! # myFtpClient = FtpClient() myListing = myFtpClient.getListing() for myEvent in myListing: if 'out' not in myEvent: continue myEvent = myEvent.replace('ftp://118.97.83.243/', '') myEvent = myEvent.replace('.out.zip', '') print 'Processing %s' % myEvent # noinspection PyBroadException try: processEvent(myEvent, myLocale) except: # pylint: disable=W0702 LOGGER.exception('Failed to process %s' % myEvent) sys.exit(0) else: processEvent(myEventId, myLocale)
my_event_id = sys.argv[1] if my_event_id in '--list': # ftp_client = FtpClient() sftp_client = SFtpClient() # myListing = ftp_client.get_listing() dir_listing = sftp_client.get_listing(my_func=is_event_id) for event in dir_listing: print event sys.exit(0) elif my_event_id in '--run-all': # # Caution, this code path gets memory leaks, use the # batch file approach rather! # ftp_client = FtpClient() dir_listing = ftp_client.get_listing() for event in dir_listing: if 'out' not in event: continue event = event.replace('ftp://118.97.83.243/', '') event = event.replace('.out.zip', '') print 'Processing %s' % event # noinspection PyBroadException try: process_event(event, my_locale) except: # pylint: disable=W0702 LOGGER.exception('Failed to process %s' % event) sys.exit(0) else: process_event(my_event_id, my_locale)
#!/usr/bin/env python # -*- coding:utf-8 -*- # Created by winchoo # 2018/8/6 from ftp_client import FtpClient f1 = FtpClient('1.1.1.1') # f1.put() #这里可以使用反射来进行判断,如果该方法已经书写则直接使用即可,如果该方法未被编写,则先进行其他的逻辑编写 if hasattr(f1, 'put'): func_get = getattr(f1, 'put') func_get() else: print('其他的逻辑')
def client_mode(): print('Client mode.') # host = input('Host:') # control_port = input('Port:') client = FtpClient() client.connect(host, control_port) client.login(username, password) type = 'A' while client: cmd = parse_command(input('ftp> ')) if len(cmd) == 1: command = cmd[0] args = '' else: command = cmd[0] args = cmd[1] # NLST, ls if command == 'ls': if args: for line in client.NLST(args): print(line) else: for line in client.NLST(): print(line) # CWD, cd elif command == 'cd': if not args: args = input('Change directory to: ') print(client.CWD(args)) # RETR elif command == 'get': print(args) client.RETR(args, type) # TYPE elif command == 'type': if args.upper() != 'A' and args.upper() != 'I': print('A or I') else: type = args.upper() print(client.send_command(f'TYPE {type}')) elif command == 'ascii': type = 'A' print(client.send_command('TYPE A')) elif command == 'bin': type = 'I' print(client.send_command('TYPE I')) elif command == 'quit' or command == 'bye': print(client.QUIT()) quit()
def testHasFile(self): """Test that the ftp client can check if a file exists""" myClient = FtpClient() myFile = '20120726022003.inp.zip' myMessage = ('Expected that %s exists on the server' % myFile) self.assertTrue(myClient.hasFile(myFile), myMessage)
def testHasFiles(self): """Test that the ftp client can check if a list of file exists""" myClient = FtpClient() myFiles = ['20120726022003.inp.zip', '20120726022003.out.zip'] myMessage = ('Expected that %s exist on the server' % myFiles) self.assertTrue(myClient.hasFiles(myFiles), myMessage)
my_event_id = sys.argv[1] if my_event_id in '--list': # myFtpClient = FtpClient() sftp_client = SFtpClient() # myListing = myFtpClient.get_listing() dir_listing = sftp_client.get_listing(my_func=is_event_id) for event in dir_listing: print event sys.exit(0) elif my_event_id in '--run-all': # # Caution, this code path gets memory leaks, use the # batch file approach rather! # myFtpClient = FtpClient() dir_listing = myFtpClient.get_listing() for event in dir_listing: if 'out' not in event: continue event = event.replace('ftp://118.97.83.243/', '') event = event.replace('.out.zip', '') print 'Processing %s' % event # noinspection PyBroadException try: process_event(event, my_locale) except: # pylint: disable=W0702 LOGGER.exception('Failed to process %s' % event) sys.exit(0) else: process_event(my_event_id, my_locale)