By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,796 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,796 IT Pros & Developers. It's quick & easy.

Checking if the computer is online

P: n/a
Hi all,

I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets
and such, I ask for your help before getting my hands dirty.

Cheers,

Mickel G.

--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi

Jul 18 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Mickel Grönroos <mi****@csc.fi> writes:
Hi all,

I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets
and such, I ask for your help before getting my hands dirty.


For windows?

<http://article.gmane.org/gmane.comp.python.windows/969>

Thomas
Jul 18 '05 #2

P: n/a
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.fi> writes:
Hi all,

I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets
and such, I ask for your help before getting my hands dirty.


For windows?

<http://article.gmane.org/gmane.comp.python.windows/969>


I want to get this done on both Windows (2000/XP) and (Redhat) Linux (7.x
or higher).

Isn't there a way to wrap a call to urllib.urlopen in some kind of timer,
so that the call fails if there is no answer in N seconds? (I.e. if
urllib.urlopen cannot open a specific web page in N seconds, then I
assume that the reason is that the computer is not online.) That would be
good enough for me. Any suggestions?

/Mickel

Jul 18 '05 #3

P: n/a
Mickel Grönroos <mi****@csc.fi> writes:
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.fi> writes:
> Hi all,
>
> I have a silly question. Is there are simple way to check if the computer
> is connected to the Internet? It seems this should be a pretty
> straight-forward thing to do, but as I am totally unfamiliar with sockets
> and such, I ask for your help before getting my hands dirty.


For windows?

<http://article.gmane.org/gmane.comp.python.windows/969>


I want to get this done on both Windows (2000/XP) and (Redhat) Linux (7.x
or higher).

Isn't there a way to wrap a call to urllib.urlopen in some kind of timer,
so that the call fails if there is no answer in N seconds? (I.e. if
urllib.urlopen cannot open a specific web page in N seconds, then I
assume that the reason is that the computer is not online.) That would be
good enough for me. Any suggestions?


Of course you can do it this way. Or even simpler with the socket
module.

But it depends on what you exactly want:
It's been quite a time that I had a dial-up connection, but IIRC,
trying network activity when the computer was not connected would result
in Windows trying to establish the connection.

To conclude: the above mentioned URL contains code which exactly answers
your original question.

Thomas
Jul 18 '05 #4

P: n/a
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.fi> writes:
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.fi> writes:

> Hi all,
>
> I have a silly question. Is there are simple way to check if the computer
> is connected to the Internet? It seems this should be a pretty
> straight-forward thing to do, but as I am totally unfamiliar with sockets
> and such, I ask for your help before getting my hands dirty.

For windows?

<http://article.gmane.org/gmane.comp.python.windows/969>


I want to get this done on both Windows (2000/XP) and (Redhat) Linux (7.x
or higher).

Isn't there a way to wrap a call to urllib.urlopen in some kind of timer,
so that the call fails if there is no answer in N seconds? (I.e. if
urllib.urlopen cannot open a specific web page in N seconds, then I
assume that the reason is that the computer is not online.) That would be
good enough for me. Any suggestions?


Of course you can do it this way. Or even simpler with the socket
module.


You wouldn't have some sample code on how I can use the socket module to
test if I can connect to a given URL? (I would't want to rely on other
modules than the ones in the standard library, so therefore I want to
try this socket approach.)

Cheers,

/Mickel

Jul 18 '05 #5

P: n/a
Mickel Grönroos <mi****@csc.fi> writes:
You wouldn't have some sample code on how I can use the socket module to
test if I can connect to a given URL? (I would't want to rely on other
modules than the ones in the standard library, so therefore I want to
try this socket approach.)


Sorry, no. But it's easy to figure out yourself.

Thomas
Jul 18 '05 #6

P: n/a
On Thursday 18 September 2003 01:10 am, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.fi> writes:
Hi all,

I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets
and such, I ask for your help before getting my hands dirty.


For windows?

<http://article.gmane.org/gmane.comp.python.windows/969>


The problem with the InternetGetConnectedState API is that, at least on older
versions of Windows/Internet Explorer, it relies on the settings of Internet
Explorer rather than the true state of the connection. In other words, it
would return an incorrect answer if Netscape was the only browser ever used.
Also, it doesn't work in a connection sharing setup (e.g. your computer is on
a home LAN that is connected to another computer with a modem.

Maybe this is way more than the OP needs, but I'll post it anyway because it's
tough to reliably detect the connected state. After much painstaking
experimentation :) I came up with a fairly decent method of knowing if the
current computer is really connected to the Internet using a combination of
all sorts of info available (works on Windows using ctypes). It turns out
that there is no single API to call, and some APIs give unreliable info, and
others will cause your modem to go online if you're not online, so they must
be avoided.

Here's a breakdown of the determination process that works for me:

- First and foremost, if your application is currently downloading or
uploading then you know you're online. At first it seems silly, but it's
actually a pretty reliable nugget of info, so my connection detection lib has
a SetOnlineHint(isOnline) API that the app calls periodically (internally my
library uses an exponential moving average to "degrade" this value over time
so that if it falls below some threshold then the library doesn't consider it
to be accurate enough to rely on and moves to the next method of detection).

- Call the GetTcpTable API and get a list of all IP addresses whose rows have
the dwState of MIB_TCP_STATE_ESTAB. Then filter out all IP addresses
127.0.0.1, 10.* 192.168.*, and 172.16* through 172.31.*. If the remaining
list is non-empty, something on this computer is most likely connected to a
public IP address, so it's fairly safe to assume you're online

- If RasEnumConnections gives a non-empty list then you're online (but don't
assume the converse is true)

- If the IsNetworkAlive API is supported and returns false, you're not online
(But you can't rely on a "yes" answer from this API)

- If all the above don't yield any definite answers, then ping a known IP
address (either by using raw sockets or by just calling os.popen('ping ...')
and reading the output. Ping is nice because ICMP packets won't cause the
modem to autodial if you're not online. My library has as a default the IP
addresses of a bunch of well known servers (e.g. DNS root servers) to use in
emergencies, but these are best avoided in all but the most extreme
emergencies when it has no other IPs to use - it collects public IP addresses
from the GetTcpTable call above, for example.

The library runs through this determination process every second or so in its
own thread, pinging at most once every 5 or 10 seconds, and the application
just queries a IsOnline API.

Convoluted, but it works, so it was worth the effort to put it in a separate
library and never figure it out again. :)

-Dave

Jul 18 '05 #7

P: n/a
On Thu, 18 Sep 2003 09:16:03 +0300 (EEST), Mickel Grönroos
<mi****@csc.fi> wrote:
I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty


'ping external_ipaddress' would be worth looking into. I'm not sure
if a computer can recognize its own ip when it's disconnected as I
haven't really tried it.

Daniel Klein
Jul 18 '05 #8

P: n/a
Mickel Grönroos <mi****@csc.fi> wrote:
Hi all, I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets
and such, I ask for your help before getting my hands dirty.


First of all you need to define what "being online" means...

Is it that your modem has dialed up your ISP and been able to
establish a PPP connection? What if you don't use a modem, and
instead have ethernet straight into your ISP's network equipment?
If the link to your ISP's closest network equipment (switch/
router/PPP dialin) is up, but the link out from that is down --
are you "online" then? Are you online if all the links within
your ISP is up, so you can reach everyone connected to the same
ISP, but their peering is broken so you can't reach anyone else?
What if there is a firewall somewhere between you and "The
Internet", blocking you from accessing many sites, are you
"online" then? How large part of Internet do you need to be able
to reach to claim that you are "online"? Or must perhaps the
Internet be able to reach *you* before you are considered to be
online? (That means that a NAT box immediately makes you offline;
I tend to like that definition. :-)

Or perhaps you should tell us *why* you believe you need to check
if the computer is connected to the Internet or not. Then we can
go directly to telling you that it is a bad idea, and suggest what
you should do instead. <0.5 wink>
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"I don't think [that word] means what you ! bellman @ lysator.liu.se
think it means." -- The Princess Bride ! Make Love -- Nicht Wahr!
Jul 18 '05 #9

P: n/a
On Thu, 18 Sep 2003, Thomas Bellman wrote:
Mickel Grönroos <mi****@csc.fi> wrote:
Hi all,
I have a silly question. Is there are simple way to check if the computer
is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets
and such, I ask for your help before getting my hands dirty.


First of all you need to define what "being online" means...

[...] Or perhaps you should tell us *why* you believe you need to check
if the computer is connected to the Internet or not. Then we can
go directly to telling you that it is a bad idea, and suggest what
you should do instead. <0.5 wink>


OK, here's what I want to do:

When the Tkinter application I am writing starts up, it checks if certain
files on the web exists and if so, it downloads them to a local directory
replacing possible earlier copies of these files. So, what I mean by
checking if the computer is online is that I want to check if I can access
these certain remote files with urllib.urlretrieve(). If I can, then I
download them. If I can't, I assume the computer is not connected to the
Internet and I skip the update phase.

The problem with just running urllib.urlretrieve() is that the execution
of the script halts as there is no timeout available for Python 2.2.2. (I
tried timeoutsocket.py to get this done, but that only worked on Windows
2000, not Redhat Linux 7.0. I also tried upgrading to Python 2.3, but that
caused some other problems, especially with the gettext module and with
the fact that my ISO-8859-1 encoded files needed a "coding" line.)

So that's that. Anybody still got a few lines of code that would get this
done on Python 2.2.2 on Windows _and_ Linux?

Cheers,

/Mickel G.

--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi

Jul 18 '05 #10

P: n/a
> On Thu, 18 Sep 2003, Thomas Bellman wrote:
Or perhaps you should tell us *why* you believe you need to check
if the computer is connected to the Internet or not. Then we can
go directly to telling you that it is a bad idea, and suggest what
you should do instead. <0.5 wink>

*I* would like to use it to run a service in the background on my
Windows machine checking the machines ip adress perhaps once an hour,
and if it was different than it is supposed to be, it should send an
email to my mail adress containing the time and the ip adress.

That should make it possible to make a stolen computer "phone home" with
enough data to track it down.

But if it tries to call up on a modem, it has sort of given itself up.
An the new "owner" might get suspicious.
regards Max M
Jul 18 '05 #11

P: n/a
Have you tried :
socket.gethostbyname(socket.gethostname())
or
socket.gethostbyname_ex(socket.getfqdn())[2]

Normally the result should be different if you're connected or not

Cheers,
Pierre

"Mickel Grönroos" <mi****@csc.fi> a écrit dans le message de
news:ma*********************************@python.or g...
On Thu, 18 Sep 2003, Thomas Bellman wrote:
Mickel Grönroos <mi****@csc.fi> wrote:
Hi all,
I have a silly question. Is there are simple way to check if the computer is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets and such, I ask for your help before getting my hands dirty.


First of all you need to define what "being online" means...

[...] Or perhaps you should tell us *why* you believe you need to check
if the computer is connected to the Internet or not. Then we can
go directly to telling you that it is a bad idea, and suggest what
you should do instead. <0.5 wink>


OK, here's what I want to do:

When the Tkinter application I am writing starts up, it checks if certain
files on the web exists and if so, it downloads them to a local directory
replacing possible earlier copies of these files. So, what I mean by
checking if the computer is online is that I want to check if I can access
these certain remote files with urllib.urlretrieve(). If I can, then I
download them. If I can't, I assume the computer is not connected to the
Internet and I skip the update phase.

The problem with just running urllib.urlretrieve() is that the execution
of the script halts as there is no timeout available for Python 2.2.2. (I
tried timeoutsocket.py to get this done, but that only worked on Windows
2000, not Redhat Linux 7.0. I also tried upgrading to Python 2.3, but that
caused some other problems, especially with the gettext module and with
the fact that my ISO-8859-1 encoded files needed a "coding" line.)

So that's that. Anybody still got a few lines of code that would get this
done on Python 2.2.2 on Windows _and_ Linux?

Cheers,

/Mickel G.

--
Mickel Grönroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Jul 18 '05 #12

P: n/a
Have you tried :
socket.gethostbyname(socket.gethostname())
or
socket.gethostbyname_ex(socket.getfqdn())[2]

Normally the result should be different if you're connected or not

Cheers,
Pierre

"Mickel Grönroos" <mi****@csc.fi> a écrit dans le message de
news:ma*********************************@python.or g...
On Thu, 18 Sep 2003, Thomas Bellman wrote:
Mickel Grönroos <mi****@csc.fi> wrote:
Hi all,
I have a silly question. Is there are simple way to check if the computer is connected to the Internet? It seems this should be a pretty
straight-forward thing to do, but as I am totally unfamiliar with sockets and such, I ask for your help before getting my hands dirty.


First of all you need to define what "being online" means...

[...] Or perhaps you should tell us *why* you believe you need to check
if the computer is connected to the Internet or not. Then we can
go directly to telling you that it is a bad idea, and suggest what
you should do instead. <0.5 wink>


OK, here's what I want to do:

When the Tkinter application I am writing starts up, it checks if certain
files on the web exists and if so, it downloads them to a local directory
replacing possible earlier copies of these files. So, what I mean by
checking if the computer is online is that I want to check if I can access
these certain remote files with urllib.urlretrieve(). If I can, then I
download them. If I can't, I assume the computer is not connected to the
Internet and I skip the update phase.

The problem with just running urllib.urlretrieve() is that the execution
of the script halts as there is no timeout available for Python 2.2.2. (I
tried timeoutsocket.py to get this done, but that only worked on Windows
2000, not Redhat Linux 7.0. I also tried upgrading to Python 2.3, but that
caused some other problems, especially with the gettext module and with
the fact that my ISO-8859-1 encoded files needed a "coding" line.)

So that's that. Anybody still got a few lines of code that would get this
done on Python 2.2.2 on Windows _and_ Linux?

Cheers,

/Mickel G.

--
Mickel Grönroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Jul 18 '05 #13

P: n/a
On Wed, 24 Sep 2003, Pierre Quentel wrote:
Have you tried :
socket.gethostbyname(socket.gethostname())
or
socket.gethostbyname_ex(socket.getfqdn())[2]

Normally the result should be different if you're connected or not


Thanks for the tip! I tried this and the second line freezes execution
when I have unplugged my Ethernet cable. So again, I need a timeout on my
socket. This is not available in Python 2.2.2. I reckon I need to try
Python 2.3.

/Mickel G

Jul 18 '05 #14

P: n/a
On Wed, 24 Sep 2003, Pierre Quentel wrote:
Have you tried :
socket.gethostbyname(socket.gethostname())
or
socket.gethostbyname_ex(socket.getfqdn())[2]

Normally the result should be different if you're connected or not


Thanks for the tip! I tried this and the second line freezes execution
when I have unplugged my Ethernet cable. So again, I need a timeout on my
socket. This is not available in Python 2.2.2. I reckon I need to try
Python 2.3.

/Mickel G

Jul 18 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.