[GH-ISSUE #117] FEATURE - Checking the internet connection #90

Closed
opened 2026-03-02 23:37:55 +03:00 by kerem · 1 comment
Owner

Originally created by @guztavoalves on GitHub (Sep 28, 2020).
Original GitHub issue: https://github.com/alexal1/Insomniac/issues/117

Hi,
When the internet connection is down, it's caused many crashes in script.

Because this, I tried a solution for this:

import socket

def check_internet_connection():
    internet = ping_conn()
    
    if not internet:
        print(COLOR_FAIL + "[!] Internet connection lost." + COLOR_ENDC)
        print(COLOR_FAIL + "[!] Checking Internet connection status..." + COLOR_ENDC)
        
        while not internet:
            countdown(60)
            internet = ping_conn()

        print(COLOR_OKGREEN + "[!] Internet connection status: OK!" + COLOR_ENDC)

def ping_conn():
    
    IPaddress=socket.gethostbyname(socket.gethostname())
    
    if IPaddress=="127.0.0.1":
        return False
    else:
        
        try:
            socket.create_connection(("1.1.1.1", 53))
            return True
        except:
            return False

I use this function to time.sleep:

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='\r')
        time.sleep(1)
        t -= 1
Originally created by @guztavoalves on GitHub (Sep 28, 2020). Original GitHub issue: https://github.com/alexal1/Insomniac/issues/117 Hi, When the internet connection is down, it's caused many crashes in script. Because this, I tried a solution for this: ``` import socket def check_internet_connection(): internet = ping_conn() if not internet: print(COLOR_FAIL + "[!] Internet connection lost." + COLOR_ENDC) print(COLOR_FAIL + "[!] Checking Internet connection status..." + COLOR_ENDC) while not internet: countdown(60) internet = ping_conn() print(COLOR_OKGREEN + "[!] Internet connection status: OK!" + COLOR_ENDC) def ping_conn(): IPaddress=socket.gethostbyname(socket.gethostname()) if IPaddress=="127.0.0.1": return False else: try: socket.create_connection(("1.1.1.1", 53)) return True except: return False ``` I use this function to time.sleep: ``` def countdown(t): while t: mins, secs = divmod(t, 60) timeformat = '{:02d}:{:02d}'.format(mins, secs) print(timeformat, end='\r') time.sleep(1) t -= 1 ```
kerem 2026-03-02 23:37:55 +03:00
Author
Owner

@Fadude commented on GitHub (Jan 6, 2021):

We have added an internet-speed checker at the beggining of the session, I dont think its reasonable to check the connection to frequent during the session, I mean, if you dont have internat connectivity, you can use Insomniac anyway, so whats the point of that connection checker?

<!-- gh-comment-id:755274333 --> @Fadude commented on GitHub (Jan 6, 2021): We have added an internet-speed checker at the beggining of the session, I dont think its reasonable to check the connection to frequent during the session, I mean, if you dont have internat connectivity, you can use Insomniac anyway, so whats the point of that connection checker?
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Insomniac#90
No description provided.