Path: csiph.com!news.fcku.it!bofh.it!tornado.fastwebnet.it!53ab2750!not-for-mail Subject: Re: ip Pubblico Newsgroups: it.comp.lang.python References: From: Smith User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 7bit Lines: 91 Message-ID: Date: Tue, 22 Mar 2016 09:17:46 +0100 NNTP-Posting-Host: 213.140.2.6 X-Complaints-To: newsmaster@fastweb.it X-Trace: tornado.fastwebnet.it 1458634666 213.140.2.6 (Tue, 22 Mar 2016 09:17:46 CET) NNTP-Posting-Date: Tue, 22 Mar 2016 09:17:46 CET Xref: csiph.com it.comp.lang.python:7627 Il 19/03/2016 21:12, Claudio_F ha scritto: > #!/usr/bin/python3 > #--------------------------------------------------------------------- > import urllib.request, smtplib, time > #--------------------------------------------------------------------- > SMTP = 'smtp.gmail.com', 465 > DESTINATARIO = '...email_destinatario...' > MITTENTE = '...email_mittente...' > USERNAME = '...username...' > PASSWORD = '...password...' > ECHOADDR = '...addr_ip_server...' > ATTESA = 600 > #--------------------------------------------------------------------- > class ErrIP(Exception): pass > class ErrMail(Exception): pass > #--------------------------------------------------------------------- > def valid_ip(x): > x = x.split('.') > return (len(x) == 4) and all(z.isdecimal() for z in x) > #--------------------------------------------------------------------- > def retrive_ip(addr): > try: > d = urllib.request.urlopen(addr).read() > d = d.decode('utf-8').strip() > if not valid_ip(d): raise ErrIP() > return d > except: > raise ErrIP('Errore recupero IP!') > #--------------------------------------------------------------------- > def send_mail(smtp, username, password, > mittente, destinatario, oggetto, testo): > try: > server = smtplib.SMTP_SSL(*smtp) > server.ehlo() > server.login(username, password) > header = 'From: %s\n' % mittente > header += 'To: %s\n' % destinatario > header += 'Subject: %s\n\n' % oggetto > msg = header + testo > server.sendmail(mittente, destinatario, msg) > server.quit() > except: > raise ErrMail('Fallito invio email!') > #--------------------------------------------------------------------- > def main(): > ip = '' > err_ip = False > count = 0 > while True: > #--------------------------------------------------------- > try: > count += 1 > print('**********************************') > print('Controllo n. ', count) > read_ip = retrive_ip(ECHOADDR) > if err_ip: > ip = '' > err_ip = False > if read_ip == ip: > print('Indirizzo IP assegnato :', ip) > else: > print('Rilevato nuovo IP :', read_ip) > oggetto = testo = 'Notifica IP : %s' % read_ip > send_mail(SMTP, USERNAME, PASSWORD, MITTENTE, > DESTINATARIO, oggetto, testo) > ip = read_ip > #--------------------------------------------------------- > except ErrIP as msg: > try: > print(msg) > if not err_ip: > msg = 'Notifica errore : ' + str(msg) > send_mail(SMTP, USERNAME, PASSWORD, MITTENTE, > DESTINATARIO, msg, msg) > err_ip = True > except ErrMail as msg: > print(msg) > #--------------------------------------------------------- > except ErrMail as msg: > print(msg) > #--------------------------------------------------------- > finally: > time.sleep(ATTESA) > #--------------------------------------------------------------------- > main() > wow... Grazie 1000