Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > it.comp.lang.python > #7621
| From | Claudio_F <clau.fin@tin.it> |
|---|---|
| Newsgroups | it.comp.lang.python |
| Subject | Re: ip Pubblico |
| Date | 2016-03-19 21:12 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nckbqo$ek3$1@gioia.aioe.org> (permalink) |
| References | <jfPGy.42660$pt.19549@tornado.fastwebnet.it> |
Il 18/03/2016 09:46, Smith ha scritto:
> Ciao a tutti,
> ho questo script e vorrei suddividerlo in funzioni per renderlo più
> sintetico possibile.
Suddividerlo in funzioni si puo`, ma il codice (a parte gli errori che
contiene) non e` minimamente in grado di far fronte alle seguenti
situazioni:
- server eco IP non raggiungibile
- dati ricevuti non validi
- server smtp non raggiungibile
Poi, oltre a non crashare, in caso di anomalia non deve inviare mail a
raffica. Un esempio puo` essere il seguente:
#!/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()
Back to it.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
ip Pubblico Smith <smith@a-team.it> - 2016-03-18 09:46 +0100
Re: ip Pubblico Max_Adamo <maxadamo@usenet.cnntp.org> - 2016-03-19 16:56 +0000
Re: ip Pubblico Max_Adamo <maxadamo@usenet.cnntp.org> - 2016-03-19 20:35 +0000
Re: ip Pubblico Claudio_F <clau.fin@tin.it> - 2016-03-19 21:12 +0100
Re: ip Pubblico Max_Adamo <maxadamo@usenet.cnntp.org> - 2016-03-19 20:44 +0000
Re: ip Pubblico Claudio_F <clau.fin@tin.it> - 2016-03-19 22:32 +0100
Re: ip Pubblico Smith <smith@a-team.it> - 2016-03-22 09:17 +0100
csiph-web