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')
#2
Discussion moved to Use of classes or functions discussion
#3
The video works for me. I have not enough experience to decide right away how to approach for some more complex task. It is like a map for me. I'll see the link you provide. After opening the beer :)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#4
Why the move? I was specifically talking about this script. The video that was mentioned suggesting that heaps should be a class, I agree with. This to me wasn't a general topic, but rather specific to this script.
#5
It's turned into a general discussion on when or when not to use clasess and yet again I cant move posts around :wall:
#6
Again, I speaking specifically to this script. Not against classes when used properly.
#7
Anyway! I cant' understand this portion of the code:

self.isactive = self.ipaddress != '127.0.0.1'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#8
it's one thing to test if your system has the networking layer present and active.  it' another thing to test if your system can access the real world internet.  for the former i try pinging 127.0.0.1.  for the latter i try pinging 2001:4860:4860::8888.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#9
I am using a script I have posted in the previous forum which returns the public IP. It connects to a web service.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#10
I ping 8.8.8.8, since it's a DNS server and tends to be very reliable.


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - Random Quote from internet menator01 0 206 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