User Tools

Site Tools


script:python:test_ftp
#!/usr/bin/env python3
from ftplib import FTP
import logging, time, os
from datetime import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


#variabili
host = 'x.x.x.x'
user = 'user'
passwd = 'password'
filename = 'testnoncancellare.txt'
filenameok = 'testnoncancellare.ok'
dirname = 'testdirnoncancellare'
smtp_server = 'smtp.office365.com'
port = 25
mail=0

user_mail = "user@domain"
password_mail = "pass"
mail_from = "mailfrom"
mail_to = "mailto"
mail_subject = "Problemi Ftp1 invio ordini"
date=datetime.now().strftime("%Y%m%d-%H:%M")
mail_body = "data: " + date + "\nTesto Mail"

# logging
logging.basicConfig(filename='/var/log/checkftp.log', level=logging.DEBUG)
logtime = time.strftime('%Y-%m-%d_%H:%M:%S')
logging.info('%s ____________________________________',logtime)


def send_mail():
    mimemsg = MIMEMultipart()
    mimemsg['From']=mail_from
    mimemsg['To']=mail_to
    mimemsg['Subject']=mail_subject
    mimemsg.attach(MIMEText(mail_body, 'plain'))
    connection = smtplib.SMTP(host='smtp.office365.com', port=587)
    connection.starttls()
    connection.login(user_mail,password_mail)
    connection.send_message(mimemsg)
    connection.quit()

with open('/tmp/' + filenameok, 'w') as file:
    file.write(date)
with open('/tmp/' + filename, 'w') as file:
    file.write(date)

try:
    logtime = time.strftime('%Y-%m-%d_%H:%M:%S')
    conn = FTP(host)
    conn.login(user, passwd)
    logging.info('%s connessione Avvenuta', logtime)
except Exception as e:
    logtime = time.strftime('%Y-%m-%d_%H:%M:%S')
    send_mail()
    logging.error('%s impossibile connettersi', logtime)
    raise os._exit(0)
try:
    conn.rmd(dirname)
except Exception as e:
    pass



try:
    logtime = time.strftime('%Y-%m-%d_%H:%M:%S')
    logging.info('%s scrivo file e directory sul server ftp', logtime)
    conn.storbinary('STOR ' + filename, open('/tmp/' + filename, 'rb'))
    conn.storbinary('STOR ' + filenameok, open('/tmp/' + filenameok, 'rb'))
    conn.mkd(dirname)
    time.sleep(10)
    conn.delete(filename)
    conn.delete(filenameok)
    conn.rmd(dirname)
    logtime = time.strftime('%Y-%m-%d_%H:%M:%S')
    logging.info('%s cancello file e directory', logtime)
except Exception as e:
    logtime = time.strftime('%Y-%m-%d_%H:%M:%S')
    logging.error('%s impossibile scrivere file e directory sul server ftp', logtime)
    send_mail()
    logging.exception(logtime)
    raise os._exit(0)
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
script/python/test_ftp.txt · Last modified: 2024/11/10 07:59 by 127.0.0.1