Python Forum
Test internet connection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Test internet connection
#1
This is not a foolproof way to test if internet is active or not, but I find that it works in at least 90% of the cases.
If someone has a better (as in works better) method, please post here.

import socket

class HasInternet:
    def __init__(self):
        self.ipaddress = socket.gethostbyname(socket.gethostname())
        self.isactive = self.ipaddress != '127.0.0.1'

if __name__ == '__main__':
    conn = HasInternet()
    if conn.isactive:
        print('Internet is active')
    else:
        print('Internet is inactive')


Messages In This Thread
Test internet connection - by Larz60+ - Oct-12-2016, 04:27 PM
RE: Test internet connection - by Yoriz - Oct-13-2016, 12:07 AM
RE: Test internet connection - by wavic - Oct-13-2016, 12:08 AM
RE: Test internet connection - by micseydel - Oct-13-2016, 12:13 AM
RE: Test internet connection - by Yoriz - Oct-13-2016, 12:15 AM
RE: Test internet connection - by micseydel - Oct-13-2016, 12:19 AM
RE: Test internet connection - by wavic - Oct-13-2016, 12:20 AM
RE: Test internet connection - by Skaperen - Oct-13-2016, 03:42 AM
RE: Test internet connection - by wavic - Oct-13-2016, 08:13 AM
RE: Test internet connection - by micseydel - Oct-13-2016, 02:54 PM
RE: Test internet connection - by wavic - Oct-13-2016, 03:10 PM
RE: Test internet connection - by Skaperen - Oct-13-2016, 11:40 PM
RE: Test internet connection - by wavic - Oct-14-2016, 02:18 AM
RE: Test internet connection - by Skaperen - Oct-14-2016, 03:08 AM
RE: Test internet connection - by snippsat - Oct-14-2016, 03:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - Random Quote from internet menator01 0 304 Apr-02-2024, 07:44 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020