Python check internet connection

7 Python code examples are found related to " check internet connection". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: dreampi.py    From dreampi with MIT License 9 votes vote down vote up
def check_internet_connection():
    """ Returns True if there's a connection """

    IP_ADDRESS_LIST = [
        "1.1.1.1",  # Cloudflare
        "1.0.0.1",
        "8.8.8.8",  # Google DNS
        "8.8.4.4",
        "208.67.222.222",  # Open DNS
        "208.67.220.220"
    ]

    port = 53
    timeout = 3

    for host in IP_ADDRESS_LIST:
        try:
            socket.setdefaulttimeout(timeout)
            socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
            return True
        except socket.error:
            pass
    else:
        logger.exception("No internet connection")
        return False 
Example 2
Source File: tools.py    From spruned with MIT License 5 votes vote down vote up
def check_internet_connection():  # pragma: no cover
    global _last_internet_connection_check
    if _last_internet_connection_check and int(time.time()) - _last_internet_connection_check < 30:
        return True

    from spruned.application.context import ctx
    from spruned.application.logging_factory import Logger
    from spruned.settings import CHECK_NETWORK_HOST
    import asyncio
    if not ctx.proxy:
        Logger.root.debug('Checking internet connectivity')
        i = 0
        while i < 10:
            import random
            host = random.choice(CHECK_NETWORK_HOST)
            socket.setdefaulttimeout(3)
            try:
                socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, 53))
                _last_internet_connection_check = int(time.time())
                return True
            except:
                i += 1
                continue
        Logger.root.debug('No internet connectivity!')
    else:
        host, port = ctx.proxy.split(':')
        Logger.root.debug('Checking proxy connectivity')
        try:
            _last_internet_connection_check = int(time.time())
            p, t = await asyncio.open_connection(host=host, port=port)
            t.close()
            return True
        except Exception:
                Logger.root.debug('Cannot connect to proxy %s:%s', host, port, exc_info=True)
    return False 
Example 3
Source File: SMWYG.py    From SMWYG-Show-Me-What-You-Got with GNU General Public License v3.0 5 votes vote down vote up
def checkInternetConnection():
		try:
			requests.get('https://www.gotcha.pw/')
		except:
			print('[!] No internet connection...Please connect to the Internet')
		else:
			print('[+] Checking Internet connection...')
			print('[+] Connection Successful <3 <3 <3') 
Example 4
Source File: check_updates.py    From SSMA with GNU General Public License v3.0 5 votes vote down vote up
def check_internet_connection():
    try:
        host = socket.gethostbyname("www.google.com")
        s = socket.create_connection((host, 80), 2)
        return True
    except:
        pass
    return False 
Example 5
Source File: wifi.py    From intel-iot-refkit with MIT License 5 votes vote down vote up
def check_internet_connection(self, url):
        '''
        Check if the target is able to connect to internet by wget
        '''
        # wget internet content
        self.target.run("rm -f index.html")
        time.sleep(1)
        for i in range(3):
            (status, output) = self.target.run("wget %s" % url, timeout=100)
            if status == 0:
                break
            time.sleep(3)
        self.target_collect_info("route")
        assert status == 0, "Error messages: %s" % self.log 
Example 6
Source File: passhunt.py    From Passhunt with GNU General Public License v3.0 5 votes vote down vote up
def checkInternetConnection():
		try:
			urllib.request.urlopen('https://cirt.net/')
		except:
			print('[!] No internet connection...Please connect to the Internet')
		else:
			print('[+] Checking Internet connection...') 
Example 7
Source File: __init__.py    From pyRevit with GNU General Public License v3.0 4 votes vote down vote up
def check_internet_connection(timeout=1000):
    """Check if internet connection is available.

    Pings a few well-known websites to check if internet connection is present.

    Args:
        timeout (int): timeout in milliseconds

    Returns:
        url if internet connection is present, None if no internet.
    """
    solid_urls = ["http://google.com/",
                  "http://github.com/",
                  "http://bitbucket.com/",
                  "http://airtable.com/",
                  "http://todoist.com/",
                  "http://stackoverflow.com/",
                  "http://twitter.com/",
                  "http://youtube.com/"]
    random.shuffle(solid_urls)
    for url in solid_urls:
        if can_access_url(url, timeout):
            return url

    return None