Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.lang.python > #7627

Re: ip Pubblico

Subject Re: ip Pubblico
Newsgroups it.comp.lang.python
References <jfPGy.42660$pt.19549@tornado.fastwebnet.it> <nckbqo$ek3$1@gioia.aioe.org>
From Smith <smith@a-team.it>
Message-ID <Kc7Iy.42858$pt.11309@tornado.fastwebnet.it> (permalink)
Date 2016-03-22 09:17 +0100

Show all headers | View raw


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

Back to it.comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

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