现在的位置: 首页 > 综合 > 正文

如何用ruby获取本机IP

2013年10月21日 ⁄ 综合 ⁄ 共 244字 ⁄ 字号 评论关闭

require 'socket'
 
def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily
 
  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end
 
p local_ip()

抱歉!评论已关闭.