gpt4 book ai didi

ruby - 使 Ruby 服务器在端口 80 上工作

转载 作者:太空宇宙 更新时间:2023-11-03 18:02:05 25 4
gpt4 key购买 nike

我正在用 Ruby 创建一个简单的 Web 服务器,它在浏览器中显示文本 LOLZ。我现在有这个:

#!/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

这按预期工作。但是,我希望它在端口 80 上工作。每当我将 2000 更改为 80,并使用 bash 启动服务器时,我都会收到此错误:

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

谁能帮帮我?谢谢

编辑:我刚刚发现这适用于 0 到 999 范围内的所有端口:S

最佳答案

1024以下的端口是保留的(也叫知名端口)。您只能以 root 身份访问它们。

$ sudo ./rServe.rb

来自 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.

来自 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 - 使 Ruby 服务器在端口 80 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1927988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com