FTP Monitor.
| 238 | |
| 239 | |
| 240 | class FTPMonitor(Monitor): |
| 241 | ''' FTP Monitor. ''' |
| 242 | |
| 243 | def __init__(self, host, port, username="", password="", interval=10): |
| 244 | super(FTPMonitor, self).__init__(interval) |
| 245 | import ftplib |
| 246 | self._ftp = ftplib.FTP() |
| 247 | self._ftp_host = host |
| 248 | self._ftp_port = port |
| 249 | self._ftp_username = username |
| 250 | self._ftp_password = password |
| 251 | self._ftp.connect(self._ftp_host, self._ftp_port) |
| 252 | self._ftp.login(self._ftp_username, self._ftp_password) |
| 253 | self._print_params( |
| 254 | ['_ftp_host', '_ftp_port', '_ftp_username', '_ftp_password']) |
| 255 | |
| 256 | def _exist_remote_file(self, path, filename, local_tmp_path): |
| 257 | import ftplib |
| 258 | try: |
| 259 | _LOGGER.debug('cwd: {}'.format(path)) |
| 260 | self._ftp.cwd(path) |
| 261 | timestamp = self._ftp.voidcmd('MDTM {}'.format(filename))[4:].strip( |
| 262 | ) |
| 263 | return [True, timestamp] |
| 264 | except ftplib.error_perm: |
| 265 | _LOGGER.debug('remote file({}) not exist.'.format(filename)) |
| 266 | return [False, None] |
| 267 | |
| 268 | def _download_remote_file(self, |
| 269 | remote_path, |
| 270 | remote_filename, |
| 271 | local_tmp_path, |
| 272 | overwrite=True): |
| 273 | local_fullpath = os.path.join(local_tmp_path, remote_filename) |
| 274 | if not overwrite and os.path.isfile(fullpath): |
| 275 | return |
| 276 | else: |
| 277 | with open(local_fullpath, 'wb') as f: |
| 278 | _LOGGER.debug('cwd: {}'.format(remote_path)) |
| 279 | self._ftp.cwd(remote_path) |
| 280 | _LOGGER.debug('download remote file({})'.format( |
| 281 | remote_filename)) |
| 282 | self._ftp.retrbinary('RETR {}'.format(remote_filename), f.write) |
| 283 | |
| 284 | def _download_remote_files(self, |
| 285 | remote_path, |
| 286 | remote_dirname, |
| 287 | local_tmp_path, |
| 288 | overwrite=True): |
| 289 | import ftplib |
| 290 | remote_dirpath = os.path.join(remote_path, remote_dirname) |
| 291 | # Check whether remote_dirpath is a file or a folder |
| 292 | try: |
| 293 | _LOGGER.debug('cwd: {}'.format(remote_dirpath)) |
| 294 | self._ftp.cwd(remote_dirpath) |
| 295 | _LOGGER.debug('{} is folder.'.format(remote_dirname)) |
| 296 | |
| 297 | local_dirpath = os.path.join(local_tmp_path, remote_dirname) |