I have written a basic program in python to basically continually monitor a website and then alert me when a change/update on the website has occurred. It just rings a buzzer on a raspberry pi to do this. When I run the program and have it monitor a test website that I make changes to, it alerts me perfectly taking anywhere from 5-30 seconds to alert me after the change on the site has occurred. However, if I leave the script running for a long period of time before making a change to the website for instance 5+ minutes the script stops running and returns this error:
socket.gaierror: [Errno -3] Temporary failure in name resolution
instantly followed by this error:
urllib.error.URLError: <urlopen error [ Errno -3] Temporary failure in name resolution>
This is the code:
import RPi.GPIO as GPIO
import time
from gpiozero import Buzzer
from datetime import datetime
import hashlib
from urllib.request import urlopen, Request
import socket
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
buzzer = Buzzer(17)
url = Request('https://somewebsite.com')
response = urlopen(url).read()
currentHash = hashlib.sha224(response).hexdigest()
def alert():
GPIO.output(21, GPIO.HIGH)
buzzer.on()
now = datetime.now()
dt_string = now.strftime("%d/%m/%y %H:%M:%S")
print ("Success | ", dt_string)
while True:
try:
response = urlopen(url).read()
currentHash = hashlib.sha224(response).hexdigest()
time.sleep(5)
response = urlopen(url).read()
newHash = hashlib.sha224(response).hexdigest()
if newHash == currentHash:
continue
else:
alert()
except socket.gaierror as e:
continue
except (KeyboardInterrupt, Exception) as e:
GPIO.output(21, GPIO.LOW)
GPIO.cleanup()
print(e)
print("ended")
I have looked into this a lot online and somewhat found what may be causing it but I don't know for sure or quite what to do to fix it. I will paste below some of the things I found online to see if anyone reading this can make better sense of things and whats going on.
"This was caused by a library's failure to close connections, leading to a large number of connections stuck in a CLOSE_WAIT state. Eventually this causes the 'Temporary failure in name resolution' error due to resource exhaustion."
"gaierror is "getaddrinfo error", and -3 is "EAI_AGAIN: The name server returned a temporary failure indication. Try again later." Your DNS is acting up. Catching socket.gaierror and retrying is a good solution." (I tried what he said by catching the error and it didn't work)
"Often, URLError is raised because there is no network connection (no route to the specified server), or the specified server doesn’t exist. In this case, the exception raised will have a ‘reason’ attribute, which is a tuple containing an error code and a text error message."
This is the full error sequence that gets printed:
<urlopen error [Errno -3] Temporary failure in name resolution>
ended **(this is the print(e) bit and print("ended") bit but the full actual sequence is below)**
Traceback (most recent call last):
File "/usr/lib/python3.7/urllib/request.py", line 1324, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/usr/lib/python3.7/http/client.py", line 1260, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1306, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1255, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1030, in _send_output
self.send(msg)
File "/usr/lib/python3.7/http/client.py", line 970, in send
self.connect()
File "/usr/lib/python3.7/http/client.py", line 1415, in connect
super().connect()
File "/usr/lib/python3.7/http/client.py", line 942, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.7/socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Documents/Website_Script.py", line 32, in <module>
response = urlopen(url).read()
File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.7/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/usr/lib/python3.7/urllib/request.py", line 543, in _open
'_open', req)
File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/lib/python3.7/urllib/request.py", line 1367, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/usr/lib/python3.7/urllib/request.py", line 1326, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>