473,390 Members | 1,579 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,390 software developers and data experts.

Checking if the computer is online

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
14 4930
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
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
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
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
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
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
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
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
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
> 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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
2
by: Daniel Orner | last post by:
Is it at all possible to use JavaScript to find out if someone's online or not? E.g. to try and fetch a URL and figure out if it failed or not? I'm trying to add some functionality to my primarily...
3
by: RS | last post by:
Is there a function that will tell you if your computer is online to the internet ?
1
by: alex | last post by:
Hi May i know how to do a check to find out how many users are online and logged on to our system? Btw, i am using vb.net. Perhaps show me some articles on this issue? regards Alex
7
by: giddy | last post by:
hi, i'm making a netowork based app. and if i have the UNC path to a local computer is there a way i can check whether the pc is online/offline?? Gideon
6
by: MattPKaiser | last post by:
I am trying to find a way to check if a computer on a network is "online" so that I can access a file on a share. In short I maintain a list of computers that have my service running (in the...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
25
by: doznot | last post by:
Let's say you want to use Moodle to teach an introductory class in PHP programming. Some of the students have little or no computer experience. In addition to background reading and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.