Permalink
Browse files

bash: Add simple python server function

Thanks Mathias and Cowboy.
  • Loading branch information...
necolas committed Apr 15, 2012
1 parent ca3fb16 commit 767fd404a397a0ab167683257ce9006af6bf595c
Showing with 19 additions and 0 deletions.
  1. +2 −0 bash/bash_profile
  2. +17 −0 bash/functions
View
@@ -6,6 +6,8 @@
[ -r "$HOME/.dotfiles/bash/exports" ] && source "$HOME/.dotfiles/bash/exports"
# Aliases
[ -r "$HOME/.dotfiles/bash/aliases" ] && source "$HOME/.dotfiles/bash/aliases"
+# Functions
+[ -r "$HOME/.dotfiles/bash/functions" ] && source "$HOME/.dotfiles/bash/functions"
# Custom bash prompt
[ -r "$HOME/.dotfiles/bash/bash_prompt" ] && source "$HOME/.dotfiles/bash/bash_prompt"
View
@@ -0,0 +1,17 @@
+# Start an HTTP server from a directory, optionally specifying the port
+function pyserver() {
+ # Get port (if specified)
+ local port="${1:-8000}"
+
+ # Open in the browser
+ open "http://localhost:${port}/"
+
+ # Redefining the default content-type to text/plain instead of the default
+ # application/octet-stream allows "unknown" files to be viewable in-browser
+ # as text instead of being downloaded.
+ #
+ # Unfortunately, "python -m SimpleHTTPServer" doesn't allow you to redefine
+ # the default content-type, but the SimpleHTTPServer module can be executed
+ # manually with just a few lines of code.
+ python -c $'import SimpleHTTPServer;\nSimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map[""] = "text/plain";\nSimpleHTTPServer.test();' "$port"
+}

0 comments on commit 767fd40

Please sign in to comment.