Created
September 27, 2012 19:11
-
-
Save MarcoSero/3795843 to your computer and use it in GitHub Desktop.
A Ruby (multithreaded) Cloud App Roulette
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'thread' | |
$o = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten; | |
$n_found = 0 | |
$mutex = Mutex.new | |
def generate | |
generated = (0...4).map{ $o[rand($o.length)] }.join; | |
end | |
def thread_body | |
Net::HTTP.start('cl.ly', 80) do |http| | |
generated = generate | |
response = http.head("/" + generated) | |
if response.code == "200" | |
$mutex.synchronize do | |
$n_found += 1 | |
end | |
puts "http://cl.ly/" + generated | |
end | |
end | |
end | |
found = false | |
i = 0 | |
while !found && i < 64000 | |
thread = Thread.new do | |
i += 1 | |
thread_body | |
end | |
thread.run | |
$mutex.synchronize do | |
if $n_found >= 20 | |
found = true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment