ruby's "python -m SimpleHTTPServer"

If I remembered right, the command below used to be the most voted one on commandlinefu.com: [cc lang=”bash”] $ python -m SimpleHTTPServer [/cc] 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): [cc lang=”bash”] $ sudo gem install rack [/cc] confirm installation (Rack 1.0 should also work for our example): [cc lang=”bash”] $ rackup –version Rack 1.1 [/cc] 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 . [cc lang=”ruby”] #!/usr/bin/env rackup #\ -E deployment use Rack::ContentLength app = Rack::Directory.new Dir.pwd run app [/cc] 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: [cc lang=”bash”] $ rackup simple_http_server.ru [/cc] 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. [cc lang=”bash”] $ chmod +x simple_http_server.ru $ export PATH=$PATH:`pwd` [/cc] to share a directory with your coworkers or friends, simply cd to that directory and execute the script: [cc lang=”bash”] $ cd /the/dir/i/want/to/share $ simple_http_server.ru [/cc] since we’re actually using rackup to execute, all the rackup options are also available, which is good: [cc lang=”bash”] $ simple_http_server.ru -h [/cc] finally, let’s end up with emulating python SimpleHTTPServer’s default behavior: [cc lang=”bash”] $ simple_http_server.ru -p 8000 [/cc]

Buy me a coffee
  • Post author: Samson Wu
  • Post link: 1555.html
  • Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
0%