Question

I'm using whois to perform and parse whois queries. The problem is: i need to perform much more. Thousands and thousands. I found async package em-whois, which sounds perfect for me. This is simple addon for whois, which relies on Ruby Fibers and em-synchrony.

My current code to perform whois queries looks like this:

...
c = Whois::Client.new(:timeout => timeout)
r = c.query(domain)
...

After I've installed em-whois, i'm trying to extend this basic example.

My code looks like this now, and still is very slow:

$: << File.dirname(__FILE__) + '/../lib'
require 'em-whois'
EM.synchrony do  
  domain = ARGV[0] || "github.com"
  $i = 0;
  $num = 100;
  arr = Array.new($num)
  # perform all queries
  begin
     puts("Inside the loop i = #$i" );
     $i += 1;
     arr[$i] = Whois.whois(domain);
  end while $i < $num
  $i = 0;
  # get all results
  begin
     $i += 1;
    puts "\r#{domain}\n#{"-" * domain.length}"
    [:available?, :status, :expires_on].each do |k|
      puts "#{k}: #{arr[$i].properties[k]}"
    end
  end while $i < $num
  EM.stop
end

How do i perform batch queries (1k of domains) using async power of whois and em-whois ?

I don't have any experience with ruby before. I'm Python/C/PHP developer. Please help.

Was it helpful?

Solution

Answer from author of the library. Example linked here.

$: << File.dirname(__FILE__) + '/../lib'
require 'em-whois'
require 'atomic'

# Asynchronous, parallel multi-domain WHOIS lookup
domains = ARGV.empty? ? ["github.com", "google.com", "bing.com", "yahoo.com", "mikejarema.com"] : ARGV
whois   = {}

EM.synchrony do  

  # Progress indicator
  EM.add_periodic_timer(0.1) do
    STDERR.print "."
  end

  # Exit condition
  EM.add_periodic_timer(0.1) do
    if domains.size == whois.keys.size
      puts ""
      whois.each do |k,v|
        if v.properties[:available?]
          puts "#{k}: available"
        else
          puts "#{k}: taken, expires #{v.properties[:expires_on]}"
        end
      end

      EM.stop
    end
  end

  # Async WHOIS lookup - fire and forget via parallel fibers
  domains.each do |i|
    Fiber.new do
      whois[i] = Whois.whois(i)
    end.resume
  end

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top