Serve a Directory via Python

By  on  

Sometimes I'm working with a test HTML file and some JavaScript but need to work off of a served space.  In that case, I sometimes need to swap out folders within MAMP Stack which leads to a maintenance nightmare.  Bleh.

I recently found out that you can serve up a directory using one Python command line directive:

# Serves the current directory at:  http://0.0.0.0:8000/
python -m SimpleHTTPServer

Excellent.  No more directory and MAMP Stack juggling.  Even better is that the command is easy to memorize so no need to constantly look it up.  Keep this in mind when you want to work on something without a big server install!

Recent Features

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

Incredible Demos

  • By
    Create a Clearable TextBox with the Dojo Toolkit

    Usability is a key feature when creating user interfaces;  it's all in the details.  I was recently using my iPhone and it dawned on my how awesome the "x" icon is in its input elements.  No holding the delete key down.  No pressing it a...

  • By
    DWRequest: MooTools 1.2 AJAX Listener & Message Display

    Though MooTools 1.2 is in its second beta stage, its basic syntax and theory changes have been hashed out. The JavaScript library continues to improve and become more flexible. Fellow DZone Zone Leader Boyan Kostadinov wrote a very useful article detailing how you can add a...

Discussion

  1. Hristo Chakarov

    https://www.npmjs.com/package/http-server

    http-server

    That’s even easier.

    • Python comes preinstalled on number of OS’s though.

      Python 3 has a different syntax too.

      I think it’s

      python -m http.server
  2. Except this only works under Python 2. The Python 3 command is slightly different.

    python -m http.server
  3. Ever seen Fenix Web Server? I’m totally bias (I’m the author), but I think it works well :-) Has a GUI and a CLI, persistent servers, and an SSH tunneling tool for securely and temporarily sharing with others.

  4. You can also do this very easily with php:

    cd ~/somewhere
    php -S localhost:8888

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!