[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Test for Internet Connection

Cory Cory

3/17/2008 1:33:00 PM

Does anyone know a way to test to see if the computer is connected to
the internet in Ruby?

I wrote a Ruby program with WWW::Mechanize, but it completely hangs Ruby
when my internet is turned off. Because Ruby does not use system
threading, there is no way to check for a timeout when WWW::Mechanizer
freezes due to not having an internet connection.
--
Posted via http://www.ruby-....

15 Answers

pjb

3/17/2008 1:46:00 PM

0

Cory Cory <cg821105@ohio.edu> writes:

> Does anyone know a way to test to see if the computer is connected to
> the internet in Ruby?
>
> I wrote a Ruby program with WWW::Mechanize, but it completely hangs Ruby
> when my internet is turned off. Because Ruby does not use system
> threading, there is no way to check for a timeout when WWW::Mechanizer
> freezes due to not having an internet connection.

require 'ping'
Ping.pingecho("google.com",10,80) # --> true or false

--
__Pascal Bourguignon__

Cory Cory

3/17/2008 2:21:00 PM

0

> require 'ping'
> Ping.pingecho("google.com",10,80) # --> true or false

This did not work. The same exact problem occurred: Ruby froze and
there was no way to unfreeze it besides connected to the internet or
force quit.
--
Posted via http://www.ruby-....

Roger Pack

3/17/2008 2:23:00 PM

0

I usually do something like this on my machine

loop do
break if system("ping google.com")
sleep 1
end
puts "Internet working!"

Cory Cory wrote:
> Does anyone know a way to test to see if the computer is connected to
> the internet in Ruby?
>
> I wrote a Ruby program with WWW::Mechanize, but it completely hangs Ruby
> when my internet is turned off. Because Ruby does not use system
> threading, there is no way to check for a timeout when WWW::Mechanizer
> freezes due to not having an internet connection.

--
Posted via http://www.ruby-....

Avdi Grimm

3/17/2008 2:28:00 PM

0

On Mon, Mar 17, 2008 at 9:32 AM, Cory Cory <cg821105@ohio.edu> wrote:
> Does anyone know a way to test to see if the computer is connected to
> the internet in Ruby?

What OS?

--
Avdi

Cory Cory

3/17/2008 3:09:00 PM

0

Roger Pack wrote:
> I usually do something like this on my machine
>
> loop do
> break if system("ping google.com")
> sleep 1
> end
> puts "Internet working!"

Even that caused Ruby to hang.
I am using Ubuntu.
--
Posted via http://www.ruby-....

Roger Pack

3/17/2008 3:14:00 PM

0

require 'resolv-replace'
might help, or google for ruby asynchronous dns
I know rev has one, and also there is a package for eventmachine that
does it.

The problem is that when it does DNS lookup it hangs forever (I'd
imagine) waiting for a response.

This might help it.

loop do
break if system("ping 64.233.187.99")
sleep 1
end
puts "Internet working!"


GL.
-R

Cory Cory wrote:
> Roger Pack wrote:
>> I usually do something like this on my machine
>>
>
> Even that caused Ruby to hang.
> I am using Ubuntu.
--
Posted via http://www.ruby-....

Avdi Grimm

3/17/2008 3:26:00 PM

0

On Mon, Mar 17, 2008 at 11:08 AM, Cory Cory <cg821105@ohio.edu> wrote:
> I am using Ubuntu.

AFAIK the "right" way to test for a network connection on Ubuntu is to
use the NetworkManager D-Bus interface. I believe there is a D-Bus
library for Ruby. A quick google for the NetworkManager D-Bus API
yields this: http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS...

--
Avdi

Cory Cory

3/17/2008 4:45:00 PM

0

> use the NetworkManager D-Bus interface. I believe there is a D-Bus
> library for Ruby. A quick google for the NetworkManager D-Bus API
> yields this:
> http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS...

The ruby-dbus package is still being developed. Most importantly, it is
not available in any deb package list. This means I cannot write code
that would be very portable with this.

Any other suggestions?
--
Posted via http://www.ruby-....

Avdi Grimm

3/17/2008 5:35:00 PM

0

On Mon, Mar 17, 2008 at 12:45 PM, Cory Cory <cg821105@ohio.edu> wrote:
> The ruby-dbus package is still being developed.

This describes nearly every Ruby library in existence.

> Most importantly, it is
> not available in any deb package list. This means I cannot write code
> that would be very portable with this.

sudo gem install dbus

If you are depending on only libraries that are deb-packaged, your
code will neither be portable (to non-deb linuxen) nor will you be
able to take advantage of many Ruby libraries (since very few are deb
packaged).

--
Avdi

Cory Cory

3/18/2008 8:01:00 PM

0

Avdi Grimm wrote:
> On Mon, Mar 17, 2008 at 12:45 PM, Cory Cory <cg821105@ohio.edu> wrote:
>> The ruby-dbus package is still being developed.
>
> This describes nearly every Ruby library in existence.

I was referring to the fact that ruby-dbus is still in a pre-beta
release form.


> sudo gem install dbus

Are you suggesting that I should just write a C function to handle
internet detection? That seems like a clean answer, but C in Ruby is
something I still have yet to learn. Seems like a good time to start.
--
Posted via http://www.ruby-....