This commit is contained in:
2025-09-03 21:22:35 +02:00
parent 39dba8f54a
commit 54cb20b6af
5 changed files with 139 additions and 86 deletions

View File

@@ -1,15 +1,16 @@
#!/usr/bin/env python3
#!.venv/bin/python
"""
Script per prelevare dati da MySQL e inviare comandi SITE FTP
"""
import mysql.connector
from utils.database.connection import connetti_db
from utils.config import users_loader as setting
from ftplib import FTP
import logging
import sys
from typing import List, Tuple
import mysql.connector
from utils.database.connection import connetti_db
from utils.config import users_loader as setting
# Configurazione logging
logging.basicConfig(
@@ -38,8 +39,8 @@ def connect_ftp() -> FTP:
ftp.login(FTP_CONFIG['user'], FTP_CONFIG['password'])
logger.info("Connessione FTP stabilita")
return ftp
except Exception as e:
logger.error(f"Errore connessione FTP: {e}")
except Exception as e: # pylint: disable=broad-except
logger.error("Errore connessione FTP: %s", e)
sys.exit(1)
def fetch_data_from_db(connection: mysql.connector.MySQLConnection) -> List[Tuple]:
@@ -63,11 +64,11 @@ def fetch_data_from_db(connection: mysql.connector.MySQLConnection) -> List[Tupl
cursor.execute(query)
results = cursor.fetchall()
logger.info(f"Prelevate {len(results)} righe dal database")
logger.info("Prelevate %s righe dal database", len(results))
return results
except mysql.connector.Error as e:
logger.error(f"Errore query database: {e}")
logger.error("Errore query database: %s", e)
return []
finally:
cursor.close()
@@ -82,14 +83,13 @@ def send_site_command(ftp: FTP, command: str) -> bool:
Returns:
bool: True if the command was sent successfully, False otherwise.
"""
"""Invia un comando SITE al server FTP"""
try:
# Il comando SITE viene inviato usando sendcmd
response = ftp.sendcmd(f"SITE {command}")
logger.info(f"Comando SITE '{command}' inviato. Risposta: {response}")
logger.info("Comando SITE %s inviato. Risposta: %s", command, response)
return True
except Exception as e:
logger.error(f"Errore invio comando SITE '{command}': {e}")
except Exception as e: # pylint: disable=broad-except
logger.error("Errore invio comando SITE %s: %s", command, e)
return False
def main():
@@ -121,7 +121,7 @@ def main():
# Costruisci il comando SITE completo
ftp_site_command = f'addu {username} {password}'
logger.info(f"Sending ftp command: {ftp_site_command}")
logger.info("Sending ftp command: %s", ftp_site_command)
# Invia comando SITE
if send_site_command(ftp_connection, ftp_site_command):
@@ -129,24 +129,24 @@ def main():
else:
error_count += 1
logger.info(f"Elaborazione completata. Successi: {success_count}, Errori: {error_count}")
logger.info("Elaborazione completata. Successi: %s, Errori: %s", success_count, error_count)
except Exception as e:
logger.error(f"Errore generale: {e}")
except Exception as e: # pylint: disable=broad-except
logger.error("Errore generale: %s", e)
finally:
# Chiudi connessioni
try:
ftp_connection.quit()
logger.info("Connessione FTP chiusa")
except Exception as e:
logger.error(f"Errore chiusura connessione FTP: {e}")
except Exception as e: # pylint: disable=broad-except
logger.error("Errore chiusura connessione FTP: %s", e)
try:
db_connection.close()
logger.info("Connessione MySQL chiusa")
except Exception as e:
logger.error(f"Errore chiusura connessione MySQL: {e}")
except Exception as e: # pylint: disable=broad-except
logger.error("Errore chiusura connessione MySQL: %s", e)
if __name__ == "__main__":
main()