Python Forum
Test internet connection - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Test internet connection (/thread-458.html)

Pages: 1 2


Test internet connection - Larz60+ - Oct-12-2016

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')



RE: Test internet connection - Yoriz - Oct-13-2016

Discussion moved to Use of classes or functions discussion


RE: Test internet connection - wavic - Oct-13-2016

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 :)


RE: Test internet connection - micseydel - Oct-13-2016

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.


RE: Test internet connection - Yoriz - Oct-13-2016

It's turned into a general discussion on when or when not to use clasess and yet again I cant move posts around :wall:


RE: Test internet connection - micseydel - Oct-13-2016

Again, I speaking specifically to this script. Not against classes when used properly.


RE: Test internet connection - wavic - Oct-13-2016

Anyway! I cant' understand this portion of the code:

self.isactive = self.ipaddress != '127.0.0.1'



RE: Test internet connection - Skaperen - Oct-13-2016

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.


RE: Test internet connection - wavic - Oct-13-2016

I am using a script I have posted in the previous forum which returns the public IP. It connects to a web service.


RE: Test internet connection - micseydel - Oct-13-2016

I ping 8.8.8.8, since it's a DNS server and tends to be very reliable.