English 中文(简体)
Making a Ruby server work on port 80
原标题:
  • 时间:2009-12-18 12:39:02
  •  标签:
  • ruby
  • port

I m creating a simple web server in Ruby, which display s the text LOLZ in the browser. I have this now:

#!/usr/bin/ruby
require  socket 

server = TCPServer.open(2000)
loop do

client = server.accept
client.puts "HTTP/1.1 200 OK
"
client.puts "Content-type: text/plain
"
client.puts "
"
client.puts "LOLZ"
client.close

end

This works as expected. However, I want it to work on port 80. Whenever I change 2000 to 80, and start the server using bash, I get this error:

unknown-00-25-4b-8c-b9-b3:rServe koningbaardxiv$ ./rServe.rb
    ./rServe.rb:4:in `initialize : Permission denied - bind(2) (Errno::EACCES)
        from ./rServe.rb:4:in `open 
        from ./rServe.rb:4

Can anyone help me? Thanks

EDIT: I just figured out that this is for all ports within a range of 0 to 999 :S

最佳回答

The ports below 1024 are reserved (also called well-known ports). You can access them only as root.

$ sudo ./rServe.rb

From http://www.iana.org/assignments/port-numbers:

The port numbers are divided into three ranges: the Well Known Ports, the Registered Ports, and the Dynamic and/or Private Ports.

The Well Known Ports are those from 0 through 1023.

From http://www.linuxquestions.org/linux/articles/Technical/Why_can_only_root_listen_to_ports_below_1024:

I do not blame those who invented the port 1024 limit, it was a natural and important security feature given how UNIX machines were used in the 1970 s and 1980 s. A typical UNIX machine allowed a bunch of not necessarily fully trusted people to log in and do stuff. You don t want these untrusted users to be able to install a custom daemon pretending to be a well-known service such as telnet or ftp since that could be used to steal passwords and other nasty things.

问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签