Sep 122006
 

I’ve played around with Python a bit, but never really made something with it. I’m not sure why, but despite the fact that it’s a powerfull language, it just doesn’t feel like something for me. Perhaps it’s the Cobolesce whitespace that you need in continued lines of code, I don’t know.

Yesterday I started playing around with Ruby. Or JRuby to be exact, but JRuby is (albeit not 100% finished) compatible with the original Ruby language. As might have guess, JRuby is a Java implementation of Ruby. With some samples on the internet, I found that you can make a (very simple) webserver. Here is the code.

require 'webrick'
include WEBrick

s = HTTPServer.new(:Port => 8181,
                   :DocumentRoot => "c:/web/docroot")
trap("INT"){ s.shutdown }
s.start

This codes uses the (standard as of Ruby-1.8.0) WEBrick package. As you can see it is very simple to use. We define the port the server should run on, we define the folder where the documents should be retrieved from and then we issue the “start” method. It already understands indexes, so if you place and index.html inside the specified folder, it will be served to you if you request the root.

Some explaining. The “require” statement imports the specified package or library. The “include” statement saves your from typing “WEBrick::” before each WEBrick statement. The object s is created as an HTTPServer instance, with port and document-root passed as parameters. Next the trap statement is used to catch the INT (interrupt, or CTRL-C) event, and the command executed when caught is “s.shutdown”. In other words: pressing CTRL-C stops our webserver.
Next the server is started. That’s it.

 Posted by at 21:05

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)