Simple test to see if internet available

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.
Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

Simple test to see if internet available

Postby Larz60+ » Mon Apr 18, 2016 12:04 am

Hello,

Here's a simple test to see if the internet is available:

IsInternetActive.py
Code: Select all
import socket

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


To test:

try_from_module.py
Code: Select all
import IsInternetActive as isi

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


Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Simple test to see if internet available

Postby micseydel » Mon Apr 18, 2016 1:26 am

Why a class with an attribute instead of a function?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Simple test to see if internet available

Postby Larz60+ » Mon Apr 18, 2016 1:31 am

Hello,

A couple of questions:

    o Why not?
    o What would you suggest, and why

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Simple test to see if internet available

Postby micseydel » Mon Apr 18, 2016 2:05 am

Suggestion: A function, which computes the result each time it's called. Because: It seems roundabout to me to create a class just to compute the result and store the variable and then have to retrieve the result from the class. Also the result would be "stale" so it's a one-off.
Code: Select all
import socket

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

Becomes
Code: Select all
import socket

def hasInternet():
    ipaddress = socket.gethostbyname(socket.gethostname())
    return self.ipaddress != '127.0.0.1'
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Simple test to see if internet available

Postby Larz60+ » Mon Apr 18, 2016 3:24 am

Hello,

Ok, sounds good.

I added the post as I have seen a lot of requests on how to check if internet was available.

A function works for me.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Simple test to see if internet available

Postby ichabod801 » Mon Apr 18, 2016 8:14 pm

Slight correction to micseydel's code (remove reference to self):

Code: Select all
import socket

def hasInternet():
    ipaddress = socket.gethostbyname(socket.gethostname())
    return ipaddress != '127.0.0.1'
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Simple test to see if internet available

Postby Ofnuts » Tue Apr 19, 2016 4:23 pm

It doesn't really check if internet is available... but merely if there is a connection to a local network. To see if internet is available, look for 'google.com' (or any other node) instead of socket.gethostname().

Btw, on this very connected computer, I still get 127.0.0.1... possibly this code here relies on the fact that the local host name is known to the external DNS service, which isn't always true on personal computers. On Windows is can possibly work because the local host name is resolved through WINS.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Simple test to see if internet available

Postby micseydel » Tue Apr 19, 2016 4:49 pm

Thanks for the catch ichabod801!

Ofnuts wrote:It doesn't really check if internet is available...

I had been wondering about this too, but hadn't had a chance to check it.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Simple test to see if internet available

Postby Larz60+ » Tue Apr 19, 2016 5:32 pm

Hello,

You are correct, it will not work if the localhost has been changed from 127.0.0.1, but will otherwise.
The Google Public DNS method will work (so long as the server is up, which is almost a given).

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Simple test to see if internet available

Postby Ofnuts » Tue Apr 19, 2016 11:05 pm

Larz60+ wrote:Hello,

You are correct, it will not work if the localhost has been changed from 127.0.0.1, but will otherwise.
The Google Public DNS method will work (so long as the server is up, which is almost a given).

Larz60+


My local hoist is 127.0.0.1. Nobody changes that, it would break too many things. And what is that Google Public DNS thing?
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Simple test to see if internet available

Postby Larz60+ » Wed Apr 20, 2016 12:39 am

Hello,

My local hoist is 127.0.0.1. Nobody changes that, it would break too many things. And what is that Google Public DNS thing?


A method that has been in use for some time is to try a socket connection to 8.8.8.8, which is the
ipv4 IP-address for Google's public dns. See https://developers.google.com/speed/public-dns/docs/using#configure_your_network_settings_to_use_google_public_dns.
see also http://stackoverflow.com/questions/3764291/checking-network-connection
which shows a variety of methods, including the one I presented as well as the 8.8.8.8 one, and more.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Simple test to see if internet available

Postby kingtermite » Wed Apr 20, 2016 10:28 pm

Looks useful.

Our router 'hiccups' every now and then and this might be a good quick test to see if internet is out when we start seeing an issue.

I'm gonna give it a try.
User avatar
kingtermite
 
Posts: 21
Joined: Thu Apr 14, 2016 11:17 pm
Location: Seattle, WA

Re: Simple test to see if internet available

Postby kingtermite » Fri Apr 22, 2016 3:37 am

OK....so I played with the script. Being new to python, I thought it would be fun to wrap a Tkinter messagebox around it too.

Code: Select all
from Tkinter import *
import tkMessageBox
import socket

def hasInternet():
    ipaddress = socket.gethostbyname(socket.gethostname())
    return (ipaddress != '127.0.0.1')
   
root = Tk().withdraw()  # hiding the main window

if hasInternet():
    tkMessageBox.showinfo("Internet Status", "Internet Connection is GOOD!")
else:
    tkMessageBox.showwarning("Internet Status", "Internet Connection is NOT WORKING!")
User avatar
kingtermite
 
Posts: 21
Joined: Thu Apr 14, 2016 11:17 pm
Location: Seattle, WA

Re: Simple test to see if internet available

Postby Larz60+ » Thu May 05, 2016 9:32 am

Hello,

I guess if your intent is to look at it, a gui's ok.

The original intent was for it to run quietly
so long if a connection was found. If not checked for the script risks crashing with an error
that doesn't properly convey the actual problem.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Simple test to see if internet available

Postby kingtermite » Thu May 05, 2016 3:39 pm

Larz60+ wrote:Hello,

I guess if your intent is to look at it, a gui's ok.

The original intent was for it to run quietly
so long if a connection was found. If not checked for the script risks crashing with an error
that doesn't properly convey the actual problem.

Larz60+

Yes, I understand. That's great for a part of another program.

My exact issue is that our router is a little spotty sometimes and my wife and I are fiddling with different devices trying to determine if device is having an issue or it's the internet in the house. My *wife* wouldn't want to test by opening a command prompt and running a command. She would rather see a UI telling her something in plain english. So that was the reason for wrapping it in tiny little UI.
User avatar
kingtermite
 
Posts: 21
Joined: Thu Apr 14, 2016 11:17 pm
Location: Seattle, WA

Re: Simple test to see if internet available

Postby Larz60+ » Wed Jun 15, 2016 3:36 pm

Hello,

Again, the intent is to provide a function that will return a decision to be used in a logic statement.

I re-post the function (thanks Micseydel & Ichabod801 for your improvements) as It serves my original intent:

Code: Select all
import socket

def hasInternet():
    ipaddress = socket.gethostbyname(socket.gethostname())
    return ipaddress != '127.0.0.1'


Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

cron