SeeleyBoothe
SeeleyBoothe

Reputation: 261

Net::http check actual page

Ok so I got some help yesterday checking and actual host to see if its available. I then wrote this.

I pass it for my server www.myhost.com and port 81. Works perfect. But what if I want to actually check a page. www.myhost.com/anypage.php? Not sure but I think the problem lies with the alternate port.

def server_up(server, port)
  http = Net::HTTP.start(server, port, {open_timeout: 5, read_timeout: 5})
  response = http.head("/")
  response.code == "200"
rescue Timeout::Error, SocketError
  false
end

Upvotes: 0

Views: 82

Answers (1)

addiedx44
addiedx44

Reputation: 2743

As tadman mentioned in the comments, you could modify your method to accept an optional path argument (below). You may want to rename the method, though, since it will no longer simply check if the server is up, but rather, also if the page exists.

def server_up(server, port, path="")
  http = Net::HTTP.start(server, port, {open_timeout: 5, read_timeout: 5})
  response = http.head("/#{path}")
  response.code == "200"
rescue Timeout::Error, SocketError
  false
end

Upvotes: 1

Related Questions