Requests `ConnectionError` with addon script I wrote

I am writing an OSMC addon to scrape next bus arrival times from my local transit system. I have written this as a kodi script, and so far it works pretty well.

However, I received a `ConnectionError’ error from requests when I tried to run my script today (Kodi.log snip below).

A couple of online sources indicate this might have something to do with DNS servers.
source 1 - reddit
source 2 - stack overflow

As I say, the script has been working fine so I know the url generally works. DNS is managed by my router, and this seems fine too (again, given that things work properly every time other than today).

A reboot solved the problem, so it seems to have been just a temporary glitch. But I’d still love to know what happened so that I can make the script more robust. Any ideas what might lead to this issue? Would a pre-check for network being up help? Is there some way I can trigger network connectivity fixes from within Python?

Full debug Kodi.log here:
(sorry, the log file is not minimal as I am unable to reproduce the issue. But the error happens around 11:50)

Python error snip below:

11:49:37 8895.619141 T:1643770912    INFO: initializing python engine.
11:49:37 8895.619141 T:1643770912   DEBUG: CPythonInvoker(9, /usr/share/kodi/addons/script.transit.nextbus/ start processing
11:49:37 8896.040039 T:1643770912  NOTICE: -->Python Interpreter Initialized<--
11:49:37 8896.041016 T:1643770912   DEBUG: CPythonInvoker(9, /usr/share/kodi/addons/script.transit.nextbus/ the source file to load is "/usr/share/kodi/addons/script.transit.nextbus/"
11:49:37 8896.041016 T:1643770912   DEBUG: CPythonInvoker(9, /usr/share/kodi/addons/script.transit.nextbus/ setting the Python path to /usr/share/kodi/addons/script.transit.nextbus:/usr/lib/python2.7:/usr/lib/python2.7/plat-arm-linux-gnueabihf:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat:/usr/lib/python2.7/dist-packages/gtk-2.0
11:49:37 8896.041016 T:1643770912   DEBUG: CPythonInvoker(9, /usr/share/kodi/addons/script.transit.nextbus/ entering source directory /usr/share/kodi/addons/script.transit.nextbus
11:49:37 8896.041016 T:1643770912   DEBUG: CPythonInvoker(9, /usr/share/kodi/addons/script.transit.nextbus/ instantiating addon using automatically obtained id of "script.transit.nextbus" dependent on version 2.14.0 of the xbmc.python api

11:50:00 8918.881836 T:1643770912   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <class 'requests.exceptions.ConnectionError'>
                                            Error Contents: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
                                            Traceback (most recent call last):
                                              File "/usr/share/kodi/addons/script.transit.nextbus/", line 87, in <module>
                                              File "/usr/share/kodi/addons/script.transit.nextbus/", line 68, in main
                                                user_prediction_set = getTransitSchedule()
                                              File "/usr/share/kodi/addons/script.transit.nextbus/", line 53, in getTransitSchedule
                                                full_prediction_set = ttc.getPredictions(routes_of_interest_params)
                                              File "/usr/share/kodi/addons/script.transit.nextbus/resources/lib/", line 63, in getPredictions
                                                predictions = getNextBusData(parametersForMultiStops)
                                              File "/usr/share/kodi/addons/script.transit.nextbus/resources/lib/", line 15, in getNextBusData
                                                r = requests.get(base_url, params=payload)
                                              File "/usr/lib/python2.7/dist-packages/requests/", line 60, in get
                                                return request('get', url, **kwargs)
                                              File "/usr/lib/python2.7/dist-packages/requests/", line 49, in request
                                                return session.request(method=method, url=url, **kwargs)
                                              File "/usr/lib/python2.7/dist-packages/requests/", line 457, in request
                                                resp = self.send(prep, **send_kwargs)
                                              File "/usr/lib/python2.7/dist-packages/requests/", line 569, in send
                                                r = adapter.send(request, **kwargs)
                                              File "/usr/lib/python2.7/dist-packages/requests/", line 407, in send
                                                raise ConnectionError(err, request=request)
                                            ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
                                            -->End of Python script error report<--
11:50:00 8918.911133 T:1957687856   DEBUG: ------ Window Init (DialogKaiToast.xml) ------
11:50:00 8919.106445 T:1643770912    INFO: Python script stopped
11:50:00 8919.106445 T:1643770912   DEBUG: Thread LanguageInvoker 1643770912 terminating
11:50:01 8920.120117 T:1736176672   DEBUG: webserver: request received for /jsonrpc
11:50:06 8924.410156 T:1957687856   DEBUG: Previous line repeats 2 times.
11:50:06 8924.411133 T:1957687856   DEBUG: ------ Window Deinit (DialogKaiToast.xml) ------

As the script is Python, you can use a try: except: block where the error is happening to gracefully handle it. You could just have it go into a loop waiting for the network to come up.

But I am not sure this was simply a case of network being unavailable? I noticed the weather app was able to download data at 11:51:59, which was very soon after my failed script attempts. So I do not think there was a problem with the network on my end.

You might have meant wait for the remote server where I am attempting to connect? This is possible, but I would not have expected an error like 'Name or service not known'.

I guess with a try | except I can log the full stack track, so that might make the problem more obvious next time it comes up.