Skip to content

Instantly share code, notes, and snippets.

@seiji
Created May 25, 2012 02:50
Show Gist options
  • Select an option

  • Save seiji/2785466 to your computer and use it in GitHub Desktop.

Select an option

Save seiji/2785466 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'socket'
if $0 == __FILE__
# write this
gs = TCPServer.open("", 12345)
socks= [gs]
addr= gs.addr
addr.shift
printf("server is on %s\n", addr.join(":"))
while true
nsock= select(socks)
next if nsock == nil
nsock[0].each do |s|
if s == gs
socks.push(s.accept)
print s, "is accepted \n"
else
str= s.gets
print str
if str =~ /^[GET|HEAD]/
path= str.split(' ')[1]
sleep(0.1)
s.write <<EOF
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: http-server.rb/0.1
Content-Length: 12
this is test
EOF
s.close
socks.delete(s)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment