Skip to content

Instantly share code, notes, and snippets.

@toretore
Created June 21, 2016 02:57
Show Gist options
  • Save toretore/24dcef14e76f66aa21d5264aaf13dd20 to your computer and use it in GitHub Desktop.
Save toretore/24dcef14e76f66aa21d5264aaf13dd20 to your computer and use it in GitHub Desktop.
require 'socket'
def log(s)
puts "#{Time.now.strftime('%H:%M:%S')}: #{s}"
end
server = TCPServer.new('', 1337)
loop do
socket = server.accept
log "New connection, new socket"
s = socket.recv(1024)
log "#{s.bytesize}b: #{s.split("\r\n")[0]}"
if socket.eof?
log "Socket closed by browser"
else
log "Sending response"
socket.puts "HTTP/1.1 200 OK"
socket.puts 'Content-Length: 0'
socket.puts
socket.close
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment