Skip to content

Instantly share code, notes, and snippets.

@schmidt
Created October 13, 2008 13:37

Revisions

  1. schmidt revised this gist Jun 1, 2010. 1 changed file with 38 additions and 16 deletions.
    54 changes: 38 additions & 16 deletions web
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,38 @@
    #!/usr/bin/env ruby

    require 'webrick'

    port = 3000

    begin
    s = WEBrick::HTTPServer.new(
    :Port => port,
    :DocumentRoot => Dir::pwd
    )
    port += 1
    end while s.listeners.size < 2

    trap("INT") { s.shutdown }
    s.start
    #!/usr/bin/env ruby

    require 'webrick'
    require 'socket'
    require 'timeout'


    # code from http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open/517638#517638
    def port_in_use?(port)
    begin
    Timeout::timeout(1) do
    begin
    s = TCPSocket.new('0.0.0.0', port)
    s.close
    return true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    return false
    end
    end
    rescue Timeout::Error
    end

    return false
    end

    def port_above(port)
    while port_in_use?(port)
    port += 1
    end
    port
    end

    s = WEBrick::HTTPServer.new(:Port => port_above(3000),
    :DocumentRoot => Dir::pwd)

    trap("INT") { s.shutdown }

    s.start
  2. schmidt created this gist Oct 13, 2008.
    16 changes: 16 additions & 0 deletions web
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    #!/usr/bin/env ruby

    require 'webrick'

    port = 3000

    begin
    s = WEBrick::HTTPServer.new(
    :Port => port,
    :DocumentRoot => Dir::pwd
    )
    port += 1
    end while s.listeners.size < 2

    trap("INT") { s.shutdown }
    s.start