Skip to content

Instantly share code, notes, and snippets.

@duykhoa
Created July 12, 2016 15:03
Show Gist options
  • Save duykhoa/04ad2dc10de00cac962e398b53d3e1c5 to your computer and use it in GitHub Desktop.
Save duykhoa/04ad2dc10de00cac962e398b53d3e1c5 to your computer and use it in GitHub Desktop.
Simple server in ruby
# Code from https://www.practicingruby.com/articles/implementing-an-http-file-server
require 'socket'
server = TCPServer.new('localhost', 2345)
loop do
socket = server.accept
request = socket.gets
response = <<-HTML
<html>
<head></head>
<body>
<h1>HELLO WORLD</h1>
</body>
</html>
HTML
socket.print "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: #{response.bytesize}\r\n" +
"Connection: close\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