Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request
- import time
- import smtplib
- from email.mime.text import MIMEText
- # Configuración del servidor SMTP y credenciales
- smtp_server = 'smtp.example.com'
- smtp_port = 587
- smtp_username = 'username'
- smtp_password = 'password'
- smtp_from_address = '[email protected]'
- smtp_to_address = '[email protected]'
- url = 'https://www.example.com' # URL a monitorear
- last_response = None # Variable para almacenar la última respuesta recibida
- while True:
- try:
- response = urllib.request.urlopen(url).read() # Hacer solicitud a la URL
- if last_response is not None and response != last_response:
- # Crear mensaje de correo electrónico
- message = MIMEText(f'La URL {url} ha cambiado.')
- message['subject'] = 'Alerta de cambio de URL'
- message['from'] = smtp_from_address
- message['to'] = smtp_to_address
- # Enviar correo electrónico
- with smtplib.SMTP(smtp_server, smtp_port) as smtp:
- smtp.ehlo()
- smtp.starttls()
- smtp.login(smtp_username, smtp_password)
- smtp.send_message(message)
- print(f'Se ha enviado una alerta de cambio de URL a {smtp_to_address}.')
- last_response = response # Actualizar la última respuesta
- except Exception as e:
- print(f'Error al hacer solicitud a la URL: {e}')
- time.sleep(60) # Esperar 60 segundos antes de hacer otra solicitud
Advertisement
Add Comment
Please, Sign In to add comment