Simplest possible rack app -> permission error

Posted by 7stud on Stack Overflow See other posts from Stack Overflow or by 7stud
Published on 2012-12-08T04:52:17Z Indexed on 2012/12/08 5:04 UTC
Read the original article Hit count: 342

Filed under:
|
|

Here's the program(1.rb) blah blah blah blah blah blah:

require 'rack'

my_rack = lambda { |env| [200, {}, ["Hello.  The time is: #{Time.now}"]] }
handler = Rack::Handler::WEBrick

handler.run(my_rack, :PORT => 12_500)

Here's the error (blah blah blah blah blah):

~/ruby_programs$ ruby 1.rb 
[2012-12-07 21:49:09] INFO  WEBrick 1.3.1
[2012-12-07 21:49:09] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin10.8.0]
[2012-12-07 21:49:09] WARN  TCPServer Error: Permission denied - bind(2)
[2012-12-07 21:49:09] WARN  TCPServer Error: Permission denied - bind(2)
/Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `initialize': Permission denied - bind(2) (Errno::EACCES)
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `new'
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:85:in `block in create_listeners'
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `each'
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/utils.rb:82:in `create_listeners'
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:82:in `listen'
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:70:in `initialize'
    from /Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:45:in `initialize'
    from /Users/7stud/.rvm/gems/ruby-1.9.3-p194@programming/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `new'
    from /Users/7stud/.rvm/gems/ruby-1.9.3-p194@programming/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `run'
    from 1.rb:5:in `<main>'
~/ruby_programs$ 

Here's line 85 of ../webrick/utils.rb:

sock = TCPServer.new(ai[3], port)

If I replace the code in 1.rb with this:

require 'socket'

server = TCPServer.new 12_000 # Server bind to port 2000

loop do
  client = server.accept    # Wait for a client to connect
  client.puts "Hello !"
  client.puts "Time is #{Time.now}"
  client.close
end

I don't get any errors, and if I enter the address:

http://localhost:12000/

in my browser, I get the expected output:

Hello !
Time is 2012-12-07 18:58:15 -0700

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rvm