how can I control in python if a file has been downloaded to generate a log and send an email?

1

for example in linux it will be controlled, it would be after downloading the file

if $? -eq0 
then
    echo "El archivo se ha descargado correctamente" | mail root
else
    echo "Ha habido un error, no se a descargado el archivo" | mail root
fi
exit 0
    
asked by antoniop 26.10.2018 в 09:21
source

1 answer

0

You can use the urllib library and check the response code. I attached example.

I am using version 3.6.6 of python, although it is also compatible for later versions.

import urllib
import urllib.request
import logging

logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO)

#La url del fichero que quieres descargar

url = 'https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-512.png'
try:
    response = urllib.request.urlopen(url)
    code = response.getcode()
    if (code == 200):
        logging.info('El fichero se ha descargado correctamente')
    else:
        print("Algo ha salido mal, no se ha descargado el fichero.")
except:
    logging.debug("Algo ha salido mal")

To send messages by e-mail you can use this Guide :

And you simply send it after the 200 code.

I hope it helps! :)

    
answered by 26.10.2018 / 10:13
source