Testing for Internet Connection

A

Alexnb

Hello internet.

I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p
 
M

Marc Christiansen

Alex Marandon said:
Trying to fetch the homepage from a few major websites (Yahoo, Google,
etc.)? If all of them are failing, it's very likely that the connection
is down

or there is a mandatory proxy...

Does it count as internet connection, when only port 80 and port 443 are
accessible and those require going through a proxy (very strict firewall
policy)? Or everything requires using SOCKS?
Just some possible problems I came up with. I don't know how often some-
thing like this will happen.

Ciao
Marc
 
T

Thomas Troeger

Alex said:
Alexnb said:
I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p

Trying to fetch the homepage from a few major websites (Yahoo, Google,
etc.)? If all of them are failing, it's very likely that the connection
is down. You can use urllib2 [1] to accomplish that.

[1] <http://docs.python.org/lib/module-urllib2.html>

This seems to work and is rather fast and wastes no bandwidth:

==============================================================================
#!/usr/bin/python

import socket, struct

def check_host(host, port, timeout=1):
"""
Check for connectivity to a certain host.
"""
# assume we have no route.
ret=False

# connect to host.
try:
# create socket.
sock=socket.socket()
# create timeval structure.
timeval=struct.pack("2I", timeout, 0)
# set socket timeout options.
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDTIMEO, timeval)
# connect to host.
sock.connect((host, port))
# abort communications.
sock.shutdown(SHUT_RDWR)
# we have connectivity after all.
ret=True
except:
pass

# try to close socket in any case.
try:
sock.close()
except:
pass

return ret

# -------------------------------- main ---------------------------------

if check_host("www.heise.de", 80):
print "Horray!"
else:
print "We've lost headquarters!"
==============================================================================

I hope the code is ok, but there is always something you can do better.
Comments? :)

Cheers,
Thomas.
 
A

Alexnb

Troeger said:
Alex said:
Alexnb said:
I am wondering, is there a simple way to test for Internet connection?
If
not, what is the hard way :p

Trying to fetch the homepage from a few major websites (Yahoo, Google,
etc.)? If all of them are failing, it's very likely that the connection
is down. You can use urllib2 [1] to accomplish that.

[1] <http://docs.python.org/lib/module-urllib2.html>

This seems to work and is rather fast and wastes no bandwidth:

==============================================================================
#!/usr/bin/python

import socket, struct

def check_host(host, port, timeout=1):
"""
Check for connectivity to a certain host.
"""
# assume we have no route.
ret=False

# connect to host.
try:
# create socket.
sock=socket.socket()
# create timeval structure.
timeval=struct.pack("2I", timeout, 0)
# set socket timeout options.
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeval)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDTIMEO, timeval)
# connect to host.
sock.connect((host, port))
# abort communications.
sock.shutdown(SHUT_RDWR)
# we have connectivity after all.
ret=True
except:
pass

# try to close socket in any case.
try:
sock.close()
except:
pass

return ret

# -------------------------------- main ---------------------------------

if check_host("www.heise.de", 80):
print "Horray!"
else:
print "We've lost headquarters!"
==============================================================================

I hope the code is ok, but there is always something you can do better.
Comments? :)

Cheers,
Thomas.

Thomas this code did not work on google.com and I also tried it with port
443
 
A

Alexnb

Alex said:
I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p

Trying to fetch the homepage from a few major websites (Yahoo, Google,
etc.)? If all of them are failing, it's very likely that the connection
is down. You can use urllib2 [1] to accomplish that.

[1] <http://docs.python.org/lib/module-urllib2.html>

What exactly do you think will work? I am not sure what you think I should
do? If I use urlopen("http://www.google.com") and I am not connected, I am
not going to get an exception, the program will fail.
 
N

norseman

Grant said:
Bullshit. You get an exception. Here's my program:

import urllib2
try:
con = urllib2.urlopen("http://www.google.com/")
data = con.read()
print data
except:
print "failed"

If I run it with no internet connection, I get this:

$ python testit.py
failed

If I bring up the internet connection, then I get a bunch of
HTML.
=============================
Yep -me two

Process:
copy/paste into afile
slide lines left to create proper indent values
save
python afile

I get same as Grant


If one does a copy/paste into interactive Python, it does fail.
(Lots of indent error messages. After all, it is Python :)


Steve
(e-mail address removed)
 
Joined
Jan 28, 2013
Messages
1
Reaction score
0
im very sorry to revive a 5 year old thread, but i needed to do this in windows..and im sorry to say you guys must have been new to python because solving this was very simple..(i cant post links..)
Code:
import urllib2
try:
    con = urllib2.urlopen("google.com")
    data = con.read()
    if "google" in data:
        print "We have connection!"
except:
    print "failed"
Works just fine
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top