Blocked host: c25f383bd08f.ngrok.io
- Step: Blocked host in Ruby.
require 'socket'
host = 'c25f383bd08f.ngrok.io'
port = 80
begin
socket = TCPSocket.new(host, port)
puts "Host is not blocked."
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
puts "Host is blocked."
ensure
socket&.close
end
The above code snippet demonstrates how to check if a specific host is blocked in Ruby. In this case, the host being checked is 'c25f383bd08f.ngrok.io'
.
- Step: Import the
socket
library.
require 'socket'
This line of code imports the socket
library, which provides a set of classes and methods for working with sockets in Ruby.
- Step: Set the host and port.
host = 'c25f383bd08f.ngrok.io'
port = 80
These lines of code assign the host and port values to variables. In this example, the host is 'c25f383bd08f.ngrok.io'
and the port is 80
.
- Step: Create a TCP socket and attempt a connection.
begin
socket = TCPSocket.new(host, port)
puts "Host is not blocked."
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
puts "Host is blocked."
ensure
socket&.close
end
This code block creates a new TCP socket using the TCPSocket.new(host, port)
method. It attempts to establish a connection with the specified host and port. If the connection is successful, it prints "Host is not blocked." Otherwise, if an Errno::ECONNREFUSED
or Errno::EHOSTUNREACH
exception is raised, it prints "Host is blocked." The ensure
block ensures that the socket is closed regardless of whether an exception is raised or not.
- Step: Close the socket.
socket&.close
This line of code closes the socket using the close
method. The &.
operator is used to avoid calling the close
method on a nil
object if an exception was raised during the connection attempt.