Guest User

Monitorizacion url y envio alerta por email

a guest
Feb 28th, 2023
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import urllib.request
  2. import time
  3. import smtplib
  4. from email.mime.text import MIMEText
  5.  
  6. # Configuración del servidor SMTP y credenciales
  7. smtp_server = 'smtp.example.com'
  8. smtp_port = 587
  9. smtp_username = 'username'
  10. smtp_password = 'password'
  11. smtp_from_address = '[email protected]'
  12. smtp_to_address = '[email protected]'
  13.  
  14. url = 'https://www.example.com' # URL a monitorear
  15. last_response = None # Variable para almacenar la última respuesta recibida
  16.  
  17. while True:
  18. try:
  19. response = urllib.request.urlopen(url).read() # Hacer solicitud a la URL
  20. if last_response is not None and response != last_response:
  21. # Crear mensaje de correo electrónico
  22. message = MIMEText(f'La URL {url} ha cambiado.')
  23. message['subject'] = 'Alerta de cambio de URL'
  24. message['from'] = smtp_from_address
  25. message['to'] = smtp_to_address
  26.  
  27. # Enviar correo electrónico
  28. with smtplib.SMTP(smtp_server, smtp_port) as smtp:
  29. smtp.ehlo()
  30. smtp.starttls()
  31. smtp.login(smtp_username, smtp_password)
  32. smtp.send_message(message)
  33. print(f'Se ha enviado una alerta de cambio de URL a {smtp_to_address}.')
  34. last_response = response # Actualizar la última respuesta
  35. except Exception as e:
  36. print(f'Error al hacer solicitud a la URL: {e}')
  37. time.sleep(60) # Esperar 60 segundos antes de hacer otra solicitud
  38.  
Advertisement
Add Comment
Please, Sign In to add comment