有人可以向我解释一下这段代码的每一部分是做什么的吗? 如果有人能给我一步一步的解释会很有帮助。 另外,我怎么能上传文件? 我如何操作一般的ruby服务器?
#!/usr/bin/env ruby
require 'socket'
require 'cgi'
server = TCPServer.new('127.0.0.1', 8888)
puts 'Listening on 127.0.0.1:8888'
loop {
client = server.accept
first_request_header = client.gets
resp = first_request_header
headers = ['http/1.1 200 ok',
"date: #{CGI.rfc1123_date(Time.now)}",
'server: ruby',
'content-type: text/html; charset=iso-8859-1',
"content-length: #{resp.length}\r\n\r\n"].join("\r\n")
client.puts headers # send the time to the client
client.puts resp
client.close
}
答案 0 :(得分:1)
#required gems
require 'socket'
require 'cgi'
#creating new connection to a local host on port 8888
server = TCPServer.new('127.0.0.1', 8888)
puts 'Listening on 127.0.0.1:8888'
loop {
#looks like a client method call to open the connection
client = server.accept
first_request_header = client.gets
resp = first_request_header
#setting the request headers
headers = ['http/1.1 200 ok',
"date: #{CGI.rfc1123_date(Time.now)}",
'server: ruby',
'content-type: text/html; charset=iso-8859-1',
"content-length: #{resp.length}\r\n\r\n"].join("\r\n")
#inserts custom client headers into request
client.puts headers
client.puts resp
#closes client connection to local host
client.close
}