Search code examples
pythongoogle-drive-apipython-importhttplib2

How to solve Unable to find the server at www.googleapis.com when not connected to the internet with a Python Application


I have a desktop app, made with python that works as a standalone application, it runs fine without relying on the internet. Only to connect to Google Drive the app requires internet connectivity.

The problem is that on running the app, when not connected to the internet, the app crashes as it contains import statements that are relating to internet connectivity.

What works: On commenting out internet connectivity related import statements, the app executes normally, in an offline environment.

What doesn't work: When the import statements are included in the code and in the absence of an internet connection, the app simply crashes. Some pseudocode below.

main.py

from kivy.app import App
'''More import statements'''
....
....
from helper import Hi

class Hello(Hi):
'''Rest of the Code goes here'''
...
...
...

helper.py

'''If the device is not connected to the internet, the following internet import statements throw an error. These imports are needed to connect Google Drive.'''

from __future__ import print_function
import httplib2
import os, io

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from apiclient.http import MediaFileUpload, MediaIoBaseDownload
try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None
import auth

class Hi:
pass
'''Rest of the Code goes here'''
...
...
...

Expectation: Need a way to enable the app to function even in the absence of an internet connection. If there is no internet connection, warn the user to connect to the internet to access Google Drive instead of simply crashing.

Error: httplib2.ServerNotFoundError: Unable to find the server at www.googleapis.com

I understand that python code is read from top to bottom, the app crashes as the import statements verify that it is not connected to the internet. Is there a way to make the app work offline, and for online activities ask the user to connect to the internet?

Appreciate your help in this matter.


Solution

  • Was able to sort this out with the help of this reference. The issue was not the presence of the import statements by a function that was referencing these statements.

    The solution is as below:

    helper.py

    from __future__ import print_function
    import httplib2
    import os, io
    
    from apiclient import discovery
    from oauth2client import client
    from oauth2client import tools
    from oauth2client.file import Storage
    from apiclient.http import MediaFileUpload, MediaIoBaseDownload
    try:
        import argparse
        flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
    except ImportError:
        flags = None
    import auth
    
    import socket
    
    class Hi:
    
        def is_connected():
            try:
            # connect to the host -- tells us if the host is actually reachable
                socket.create_connection(("www.google.com", 80))
                print("Connected to the internet, execute code.")
                self.fileUpload() #----- function is called, if host is reachable.
            except OSError:
                print("No internet, code not executed.")
            
       def fileUpload():
           # This is the function that requires the import statements
           ...
           ...