2009-12-18 8 views
1

Je crée un serveur web simple dans Ruby, qui affiche le texte LOLZ dans le navigateur. J'ai maintenant ceci:Faire fonctionner un serveur Ruby sur le port 80

#!/usr/bin/ruby 
require 'socket' 

server = TCPServer.open(2000) 
loop do 

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

end 

Cela fonctionne comme prévu. Cependant, je veux travailler sur le port 80. Chaque fois que je change 2000 à 80, et démarrer le serveur en utilisant bash, je reçois cette erreur:

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 

Quelqu'un peut-il me aider? Merci

EDIT: Je viens de comprendre que ceci est pour tous les ports dans une plage de 0 à 999: S

+0

Y at-il déjà quelque chose (Apache) écoute sur le port 80? – yfeldblum

+0

Nope ce n'est pas ......... –

Répondre

4

Les ports inférieurs à 1024 sont réservés (également appelés ports bien connus). Vous pouvez y accéder uniquement en tant que root.

$ sudo ./rServe.rb 

De 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.

De 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.

+0

Cela a fait l'affaire! Merci! –

Questions connexes