我想通过 Ruby 在我的 LAN 上生成(我不喜欢创建单词)本地服务器(如 localhost/8000),我研究了 ? 互联网,但我无法做到。我的目标是在本地服务器中显示 html 页面,使用 Ruby。我怎样才能做到?
require 'socket'
Run Code Online (Sandbox Code Playgroud)
我在标准库中使用套接字,但在刷新页面时出现错误。
require 'socket'
server = TCPServer.new('localhost', 2345)
loop do
socket = server.accept
request = socket.gets
STDERR.puts request
response = "Hello World!\n"
socket.print "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: #{response.bytesize}\r\n" +
"Connection: close\r\n"
socket.print "\r\n"
socket.print response
socket.close
end
Run Code Online (Sandbox Code Playgroud)