ruby’s “python -m SimpleHTTPServer”

If I remembered right, the command below used to be the most voted one on commandlinefu.com:

$ python -m SimpleHTTPServer

the “SimpleHTTPServer” module is written in python and it maybe called “the python way”, but that’s not our topic today. what we really want to do here is just making a ruby equivalent for this popular command, using rack.

first, make sure you have rack gem installed on your system (if you’ve installed rails 2.x before, you may already have it):

$ sudo gem install rack

confirm installation (Rack 1.0 should also work for our example):

$ rackup --version
Rack 1.1

alright, preparation is done, let’s start cooking. only a rackup file will be sufficient, for consistency, let’s just call it simple_http_server.ru .

#!/usr/bin/env rackup
#\ -E deployment

use Rack::ContentLength

app = Rack::Directory.new Dir.pwd
run app


Rack::Directory is so awesome that we just simply rely on it… aren’t i cheating? hehe, maybe…

ok, we’re done. to test it, simply run:

$ rackup simple_http_server.ru

and then point your browser to http://localhost:9292/ .

to be useful for daily use, you may consider chmod +x it to make it executable and put it in your $PATH.

$ chmod +x simple_http_server.ru
$ export PATH=$PATH:`pwd`

to share a directory with your coworkers or friends, simply cd to that directory and execute the script:

$ cd /the/dir/i/want/to/share
$ simple_http_server.ru

since we’re actually using rackup to execute, all the rackup options are also available, which is good:

$ simple_http_server.ru -h

finally, let’s end up with emulating python SimpleHTTPServer’s default behavior:

$ simple_http_server.ru -p 8000

历史上的今天:

Related posts:

14 Comments on "ruby’s “python -m SimpleHTTPServer”"

  1. anfernee China Mozilla Firefox Ubuntu Linux says:

    I googled ‘ruby simple http server’, google gave me this link. It is exactly what i want. thank you, samson! haha

    Your blog is awesomeeeeeee!!!!

  2. Ardi Indonesia Google Chrome Linux says:

    i found $python -m SimpleHTTPServer on commandlinefu.com
    and for ruby i found this site by google,
    thanks.

  3. Terry Australia Google Chrome Linux says:

    1 line
    rackup -b “run Rack::Directory.new ‘.'”

  4. Anneliese Internet Explorer Windows says:

    I am really impressed with your writing skills as well as
    with the layout on your blog. Is this a paid theme or did you
    customize it yourself? Either way keep up the nice quality writing, it is rare to
    see a nice blog like this one these days.

  5. Magne Norway Google Chrome Mac OS says:

    Awesome, thanks! Works like a charm.

    Two things:

    First, you have a small typo here: http://locahost:9292/

    Second:
    You might want to note that you can get the script to run from any folder, by moving the ‘simple_http_server.ru’ file into the ‘/usr/local/bin’ folder on OSX. Just make sure that ‘/usr/local/bin’ is listed when doing ‘echo $PATH’

    I would rather do that, than add the current folder you’re in to the PATH variable with the command you suggested: ‘export PATH=$PATH:`pwd`

  6. Don M United States Google Chrome Mac OS says:

    Thank you for posting this. I am a pythonista who is learning Ruby. I asked myself the question today: Does Ruby have a SimpleHTTPServer equivalent? I turned to a Google search for answers and found this page. Your solution functions just as expected.

  7. brilliant website we at this point what is your
    thoughts on mine web page dealing with

  8. wonderful post you’ve got at this point what is you’re first impressions with
    our web post in respect to

Trackbacks for this post

  1. Bardzo prosty serwer http indexem katalogów « QuaSeaLab Blog Poland WordPress Unknow Os

Got something to say? Go for it!

使用新浪微博登陆