开发者

Check if Internet Connection Exists with Ruby?

开发者 https://www.devze.com 2022-12-22 04:02 出处:网络
Just asked how to check if an internet connection exists using javascript and got some great answers.What\'s the easiest way to do this in Ruby?In trying to make generated html markup code as clean as

Just asked how to check if an internet connection exists using javascript and got some great answers. What's the easiest way to do this in Ruby? In trying to make generated html markup code as clean as possible, I'd like to conditionally render the script tag for javascript files depending on whether or not an internet condition. Something like (this is HAML):

- if internet_connection?
    %script{:src => "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", :type => "text/javascript"}
- else
    %script{:src => "/shared/javascripts/jquery/jquery.js开发者_开发知识库", :type => "text/javascript"}


require 'open-uri'

def internet_connection?
  begin
    true if open("http://www.google.com/")
  rescue
    false
  end
end

This is closer to what the OP is looking for. It works in Ruby 1.8 and 1.9. It's a bit cleaner too.


I love how everyone simply assume that googles servers are up. Creds to google.

If you want to know if you have internet without relying on google, then you could use DNS to see if you are able to get a connection.

You can use Ruby DNS Resolv to try to translate a url into an ip address. Works for Ruby version 1.8.6+

So:

#The awesome part: resolv is in the standard library

def has_internet?
  require "resolv"
  dns_resolver = Resolv::DNS.new()
  begin
    dns_resolver.getaddress("symbolics.com")#the first domain name ever. Will probably not be removed ever.
    return true
  rescue Resolv::ResolvError => e
    return false
  end
end

Hope this helps someone out :)


You can use the Ping class.

require 'resolv-replace'
require 'ping'

def internet_connection?
  Ping.pingecho "google.com", 1, 80
end

The method returns true or false and doesn't raise exceptions.


Same basics as in Simone Carletti's answer but compatible with Ruby 2:

# gem install "net-ping"

require "net/ping"

def internet_connection?
  Net::Ping::External.new("8.8.8.8").ping?
end


require 'open-uri'

page = "http://www.google.com/"
file_name = "output.txt"
output = File.open(file_name, "a")
begin
  web_page = open(page, :proxy_http_basic_authentication => ["http://your.company.proxy:80/", "your_user_name", "your_user_password"])  
  output.puts "#{Time.now}: connection established - OK !" if web_page
rescue Exception
  output.puts "#{Time.now}: Connection failed !"
  output.close
ensure
  output.close
end


I was trying to find a solution to a problem similar to yours and could not find any. Unfortunately the Ping.pingecho method doesn't work for me for some reason i don't know. I came up with a solution. The latest way to do it using httparty. I wanted this in a module and so did it this way and it works just fine

# gem install httparty
require "httparty"

module Main
  def Main.check_net
    begin
      a = HTTParty.get("https://www.google.com")
      if a.length() >= 100
        puts "online"
      end
    rescue SocketError
      puts "offline"
    end
  end
end

include Main
Main.check_net

A socket error to Google might not happen so this method will work


def connected?
  !!Socket.getaddrinfo("google.com", "http")  
rescue SocketError => e
  e.message != 'getaddrinfo: nodename nor servname provided, or not known'
end

Since it uses a hostname the first thing it needs to do is DNS lookup, which causes the exception if there is no internet connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消