Skip to content

Instantly share code, notes, and snippets.

@kopylovvlad
Created October 12, 2018 18:54
Show Gist options
  • Save kopylovvlad/259cda121db785cd1f02f2280f6d6cc3 to your computer and use it in GitHub Desktop.
Save kopylovvlad/259cda121db785cd1f02f2280f6d6cc3 to your computer and use it in GitHub Desktop.
require 'socket'
# run on localhost:3000
server = TCPServer.new('localhost', 3000)
loop do
socket = server.accept
request = socket.gets
method, path = request.split
sleep_time = rand(5)
# some calculation is here
sleep(sleep_time)
response = "Hello World!\n" +
"your request: #{request}" +
"method: #{method.inspect}\n" +
"path: #{path.inspect}\n" +
"sleep time: #{sleep_time}\n"
socket.print "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Connection: close\r\n" +
"Content-Length: #{response.bytesize}\r\n"
socket.print "\r\n"
socket.print response
socket.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment