Created
July 12, 2016 15:03
-
-
Save duykhoa/04ad2dc10de00cac962e398b53d3e1c5 to your computer and use it in GitHub Desktop.
Simple server in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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