返回顶部

收藏

ruby实现的一个超简单的http server

更多
require 'socket'

server = TCPServer.open 9000
puts "Listening on port 9000"

loop {
  client = server.accept()
  while((x = client.gets) != "\r\n")
    puts x
  end
  resp = "Here be dragons"
  headers = ["HTTP/1.1 200 OK",
             "Date: Tue, 14 Dec 2010 10:48:45 GMT",
             "Server: Ruby",
             "Content-Type: text/html; charset=iso-8859-1",
             "Content-Length: #{resp.length}\r\n\r\n"].join("\r\n")
  client.puts headers
  client.puts resp
  client.close
  puts "Request Handled"
}

标签:http,server,http服务,ruby

收藏

0人收藏

支持

0

反对

0

发表评论