473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Testing for Internet Connection


Hello internet.

I am wondering, is there a simple way to test for Internet connection? If
not, what is the hard way :p
--
View this message in context: http://www.nabble.com/Testing-for-In...p18460572.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 15 '08 #1
11 10782
Alexnb wrote:
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>
Jul 15 '08 #2
Alexnb <al********@gmail.comwrites:
I am wondering, is there a simple way to test for Internet
connection? If not, what is the hard way :p
Refine the question: What do you mean by "internet"? It isn't a single
entity.

Do you mean "some particular internet host responding on a particular
network port"?

If you can define exactly what you mean by "internet connection", the
test for it becomes correspondingly easier.

--
\ “Why should I care about posterity? What's posterity ever done |
`\ for me?” —Groucho Marx |
_o__) |
Ben Finney
Jul 15 '08 #3
Alex Marandon wrote:
Alexnb wrote:
>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.
Jul 15 '08 #4
Alex Marandon <in*****@nowhere.invalid.orgwrote:
Alexnb wrote:
>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
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
Jul 15 '08 #5

Ben Finney-2 wrote:

Alexnb <al********@gmail.comwrites:
>I am wondering, is there a simple way to test for Internet
connection? If not, what is the hard way :p
Refine the question: What do you mean by "internet"? It isn't a single
entity.

Do you mean "some particular internet host responding on a particular
network port"?

If you can define exactly what you mean by "internet connection", the
test for it becomes correspondingly easier.

--
\ “Why should I care about posterity? What's posterity everdone |
`\ for me?” —Groucho Marx |
_o__) |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list
Well, really I just need to figure out if I am able to connect to one site.
That site is dictionary.com.
--
View this message in context: http://www.nabble.com/Testing-for-In...p18468350.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 15 '08 #6
>If you can define exactly what you mean by "internet connection", the
>test for it becomes correspondingly easier.
Well, really I just need to figure out if I am able to connect
to one site. That site is dictionary.com.
Then use urllib2 to try to fetch a page from dictionary.com. If
it works, you're "connected". If it fails, you're not. The
most straight-forward way to find out if you can do X is to try
to do X.

--
Grant Edwards grante Yow! if it GLISTENS,
at gobble it!!
visi.com
Jul 15 '08 #7

Troeger Thomas (Ext) wrote:
>
Alex Marandon wrote:
>Alexnb wrote:
>>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.
--
http://mail.python.org/mailman/listinfo/python-list

Thomas this code did not work on google.com and I also tried it with port
443
--
View this message in context: http://www.nabble.com/Testing-for-In...p18468756.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 15 '08 #8

Alex Marandon-3 wrote:
>
Alexnb wrote:
>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>
--
http://mail.python.org/mailman/listinfo/python-list

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.

--
View this message in context: http://www.nabble.com/Testing-for-In...p18471183.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 15 '08 #9
On 2008-07-15, Alexnb <al********@gmail.comwrote:
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.
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.

--
Grant Edwards grante Yow! I'm ZIPPY the PINHEAD
at and I'm totally committed
visi.com to the festive mode.
Jul 15 '08 #10

Grant Edwards wrote:
On 2008-07-15, Alexnb <al********@gmail.comwrote:
>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.

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
no******@hughes.net
Jul 15 '08 #11
On 2008-07-15, norseman <no******@hughes.netwrote:
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 :)
I'm always a bit conflicted about how to post code snippets.
IMO, the posting is a lot more readable if the code is indented
to offset it from the prose, but it does make more work for
anybody who wants to run the example.

--
Grant Edwards grante Yow! Did I SELL OUT yet??
at
visi.com
Jul 15 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Cliff Wells | last post by:
Hi, I'm writing an application that needs to know if an Internet connection is available. Basically, I want to have something similar to what a lot of email clients have, where the app can work...
8
by: Jozef | last post by:
Hello, Is there a way to test that an internet connection exists and that you are able to see the webserver before performing any connections? The reason being, when I try to connect to an SQL...
3
by: Jonny | last post by:
Hi, Please could you tell me how to check for an internet connection in C. I'm using Windows 2000. Many Thanks, Jonny
2
by: PeterH | last post by:
I am developing a web service client as a VB.Net Windows Forms application. I need to test whether the computer running my application has an open Internet connection and, if so, whether it is a...
5
by: Mark C | last post by:
I have a cable internet and would like to view my web site as if I was on a dial-up for testing purposes Is there anyway you could slow down your internet connection Regards Mark Crosbie
2
by: CLM | last post by:
When I test the remote access at work (lapttop computer to desk top computer) the connection works fine. When I bring the laptop home and try to connect to my desktop I get the error msg: The...
6
by: =?Utf-8?B?QnNtZW5nZW4=?= | last post by:
I am trying to make sure that the local connection is up. I have presently been using the NetworkChange.NetworkAvailabilityChanged Event for this. Is there a better way to do this? Also, I...
1
by: =?Utf-8?B?QiBTaW5naA==?= | last post by:
Hi Scenario 1) Host - XP Laptop with internet connection using Sky Broadband Wireless Router Netgear DG834GT 2) MS Virtual Server 2005 - I have deployed an XP MS Virtual Server 2005 to act...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all mister, Which is THE BEST WAY IN THE WORLD AROUND for: 1. detect Network
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.