473,770 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4975
Mickel Grönroos <mi****@csc.f i> 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.o rg/gmane.comp.pyth on.windows/969>

Thomas
Jul 18 '05 #2
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.f i> 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.o rg/gmane.comp.pyth on.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.f i> writes:
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.f i> 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.o rg/gmane.comp.pyth on.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.f i> writes:
On Thu, 18 Sep 2003, Thomas Heller wrote:
Mickel Grönroos <mi****@csc.f i> 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.o rg/gmane.comp.pyth on.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.f i> 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.f i> 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.o rg/gmane.comp.pyth on.windows/969>


The problem with the InternetGetConn ectedState 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(i sOnline) 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_E STAB. 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 RasEnumConnecti ons 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.f i> 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_ipaddr ess' 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.f i> 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.f i> 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.urlretri eve(). 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.urlretri eve() is that the execution
of the script halts as there is no timeout available for Python 2.2.2. (I
tried timeoutsocket.p y 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

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

Similar topics

10
3691
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. Andrew dalke@dalkescientific.com
2
4341
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 online program to also work offline, but I'd need this sort of thing to make it work well. Thanks!
3
1159
by: RS | last post by:
Is there a function that will tell you if your computer is online to the internet ?
1
950
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
3292
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
2688
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 client application) and in my client program I allow the user to change the computer that they are accessing. If a computer that is on the list becomes unplugged and I try to access a file with a FileStream the filestream creation takes 8-10...
125
6613
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 problem. http://www.jilp.org/vol9/v9paper10.pdf Specifically, they measured the overhead of a bounds
25
2844
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 topics-oriented assignments supplied by Moodle, you want to build an online text editor into the course, so students can type their PHP programs and HTML directly into files on the server, so they don't have to fight with NotePad on Windows PCs in a lab, and...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.