Friday 29 March 2013

Using Python SimpleHTTPServer to share files

There are cases when I want to share some files within the intranet to other users.

Python comes with a simple built-in module called SimpleHTTPServer that can turn any directory in the system into the web server directory. I literally just need a single command line to accomplish this task, given Python is shipped with openSUSE!

Assuming I am running linux, and I want to share my home directory ~
cd ~
Start up the http server on port 10001
youyang@monkey-dev-01:~> python -m SimpleHTTPServer 10001
Now the http server is running on port 10001. Open a browser and type the following address, where monkey-dev-01 happens to be my hostname
http://monkey-dev-01:10001
Since there is no index.html under my home directory, the files in the home directory will be listed. Now my purpose is served, but there is one slight problem -- the program is running on the foreground, meaning I can't do anything on that open terminal as it continues to display the http requests when I browse the directories. To change the running process from foreground to background, I need to press CTRL+Z to pause the process, as shown below
^Z
[1]+  Stopped                 python -m SimpleHTTPServer 10001
and type 'bg' to move the process to run in the background
youyang@monkey-dev-01:~> bg
[1]+ python -m SimpleHTTPServer 10001 &
We can also use 'jobs -l' to display the jobs in current session
youyang@monkey-dev-01:~> jobs -l
[1]+  4127 Running                 python -m SimpleHTTPServer 10001 &
We can also start up the http server running in the background directly, by appending a '&' at the end of the command
youyang@monkey-dev-01:~> python -m SimpleHTTPServer 10001&

No comments:

Post a Comment