Skip to content

Instantly share code, notes, and snippets.

@nownabe
Last active March 29, 2016 09:22
Show Gist options
  • Save nownabe/53c6caeef9274faf0e05 to your computer and use it in GitHub Desktop.
Save nownabe/53c6caeef9274faf0e05 to your computer and use it in GitHub Desktop.
Simple Sleep Web Server
#!/usr/bin/env ruby
#
# Usage:
# ruby sleep_server.rb [options]
#
# Options:
# -p port (default: 80)
# -b bind_address (default: 0.0.0.0)
# -s sleep_seconds (default: 5)
require "optparse"
require "socket"
require "webrick"
class SleepServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
sleep $sleep
res["Content-Type"] = "text/plain";
res.body = Socket.gethostname
end
end
opts = ARGV.getopts("p:b:s:")
port = opts["p"].to_i || 80
bind = opts["b"] || "0.0.0.0"
$sleep = opts["s"].to_i || 5
server = WEBrick::HTTPServer.new(:Port => port, :BindAddress => bind)
server.mount("/", SleepServlet)
trap(:INT) { server.shutdown }
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment