lint con ruff
This commit is contained in:
@@ -3,29 +3,22 @@
|
||||
Script per prelevare dati da MySQL e inviare comandi SITE FTP
|
||||
"""
|
||||
|
||||
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
|
||||
from ftplib import FTP
|
||||
|
||||
import mysql.connector
|
||||
|
||||
from utils.config import users_loader as setting
|
||||
from utils.database.connection import connetti_db
|
||||
|
||||
# Configurazione logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Configurazione server FTP
|
||||
FTP_CONFIG = {
|
||||
'host': 'localhost',
|
||||
'user': 'admin',
|
||||
'password': 'batt1l0',
|
||||
'port': 2121
|
||||
}
|
||||
FTP_CONFIG = {"host": "localhost", "user": "admin", "password": "batt1l0", "port": 2121}
|
||||
|
||||
|
||||
def connect_ftp() -> FTP:
|
||||
"""
|
||||
@@ -35,15 +28,16 @@ def connect_ftp() -> FTP:
|
||||
"""
|
||||
try:
|
||||
ftp = FTP()
|
||||
ftp.connect(FTP_CONFIG['host'], FTP_CONFIG['port'])
|
||||
ftp.login(FTP_CONFIG['user'], FTP_CONFIG['password'])
|
||||
ftp.connect(FTP_CONFIG["host"], FTP_CONFIG["port"])
|
||||
ftp.login(FTP_CONFIG["user"], FTP_CONFIG["password"])
|
||||
logger.info("Connessione FTP stabilita")
|
||||
return ftp
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
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]:
|
||||
|
||||
def fetch_data_from_db(connection: mysql.connector.MySQLConnection) -> list[tuple]:
|
||||
"""
|
||||
Fetches username and password data from the 'ftp_accounts' table in the database.
|
||||
|
||||
@@ -73,6 +67,7 @@ def fetch_data_from_db(connection: mysql.connector.MySQLConnection) -> List[Tupl
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
|
||||
def send_site_command(ftp: FTP, command: str) -> bool:
|
||||
"""
|
||||
Sends a SITE command to the FTP server.
|
||||
@@ -88,10 +83,11 @@ def send_site_command(ftp: FTP, command: str) -> bool:
|
||||
response = ftp.sendcmd(f"SITE {command}")
|
||||
logger.info("Comando SITE %s inviato. Risposta: %s", command, response)
|
||||
return True
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
logger.error("Errore invio comando SITE %s: %s", command, e)
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""
|
||||
Main function to connect to the database, fetch FTP user data, and send SITE ADDU commands to the FTP server.
|
||||
@@ -119,7 +115,7 @@ def main():
|
||||
username, password = row
|
||||
|
||||
# Costruisci il comando SITE completo
|
||||
ftp_site_command = f'addu {username} {password}'
|
||||
ftp_site_command = f"addu {username} {password}"
|
||||
|
||||
logger.info("Sending ftp command: %s", ftp_site_command)
|
||||
|
||||
@@ -131,7 +127,7 @@ def main():
|
||||
|
||||
logger.info("Elaborazione completata. Successi: %s, Errori: %s", success_count, error_count)
|
||||
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
logger.error("Errore generale: %s", e)
|
||||
|
||||
finally:
|
||||
@@ -139,14 +135,15 @@ def main():
|
||||
try:
|
||||
ftp_connection.quit()
|
||||
logger.info("Connessione FTP chiusa")
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
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: # pylint: disable=broad-except
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
logger.error("Errore chiusura connessione MySQL: %s", e)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user