Engine application urlfetch throws exceptions if I think it shouldn't be

I wrote my first Python application with the App Engine API, it is designed to keep track of the list of servers and notifications when one of them drops, sending a message to my iPhone using Prowl or sending me an email or both.

The problem is that several times a week she notifies me that the server is not working, even when this is clearly not the case. I tested it on servers that, as I know, should be almost all the time, for example google.com or amazon.com, but I also get notifications with them.

I have a copy of code working on http://aeservmon.appspot.com , you can see that google.com was added on January 3, but is only listed for 6 days.

Below is the corresponding section of the code from checkservers.py that checks using urlfetch, I assumed that the DownloadError exception will be raised only when the server cannot be contacted, but maybe I'm wrong.

What am I missing?

Full github source under mrsteveman1 / aeservmon (I can only post one link as a new user, sorry!)

def testserver(self,server):
     if server.ssl:
          prefix = "https://"
     else:
          prefix = "http://"
     try:
          url = prefix + "%s" % server.serverdomain
          result = urlfetch.fetch(url, headers = {'Cache-Control' : 'max-age=30'} )
     except DownloadError:
          logging.info('%s could not be reached' % server.serverdomain)
          self.serverisdown(server,000)
          return
     if result.status_code == 500:
          logging.info('%s returned 500' % server.serverdomain)
          self.serverisdown(server,result.status_code)
     else:
          logging.info('%s is up, status code %s' % (server.serverdomain,result.status_code))
          self.serverisup(server,result.status_code)

UPDATE January 21:

Today I found one of the exceptions in the logs:

ApplicationError: 5 
Traceback (most recent call last):
  File "/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 507, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/aeservmon/1.339312180538855414/checkservers.py", line 149, in get
    self.testserver(server)
  File "/base/data/home/apps/aeservmon/1.339312180538855414/checkservers.py", line 106, in testserver
    result = urlfetch.fetch(url, headers = {'Cache-Control' : 'max-age=30'} )
  File "/base/python_lib/versions/1/google/appengine/api/urlfetch.py", line 241, in fetch
    return rpc.get_result()
  File "/base/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 501, in get_result
    return self.__get_result_hook(self)
  File "/base/python_lib/versions/1/google/appengine/api/urlfetch.py", line 331, in _get_fetch_result
    raise DownloadError(str(err))
DownloadError: ApplicationError: 5 
+3
source share

Source: https://habr.com/ru/post/1729311/


All Articles