Skip to content

Instantly share code, notes, and snippets.

@embs
Created March 21, 2012 02:28
Show Gist options
  • Save embs/2143839 to your computer and use it in GitHub Desktop.
Save embs/2143839 to your computer and use it in GitHub Desktop.
This is a tiny ruby web server.
require 'socket'
webserver = TCPServer.new('127.0.0.1', 7777)
puts "Iniciando servidor."
while (session = webserver.accept)
session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
begin
request = session.gets
rescue Errno::ECONNRESET
session = webserver.accept
end
trimmedrequest = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
filename = trimmedrequest.chomp
if filename == ""
filename = "index.html"
end
begin
displayfile = File.open(filename, 'r')
content = displayfile.read()
session.print content
rescue Errno::ENOENT
session.print "File not found"
end
session.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment