Monday, August 10, 2009

[Python] Easily make available some files through HTTP

At work, our desktop computers run on different OSes, mainly Linux and Windows.

To be able to share files between Linux and Windows, a samba server will need to be installed and configured on the Linux box.

Since I dont need any files to be permanently shared, I dont have samba configured. If I need to make some files accessible, I use the below script to create a simple python-powered HTTP server, so that the files I want to share are easily accessible for download through HTTP.


#!/usr/bin/python

import SimpleHTTPServer
import SocketServer

# minimal web server. serves files relative to the
# current directory.

#Replace the port number if the port 8000 is in use already
PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()



-Just save the below script (i.e. share_files.py) under the directory you want the World to have access to (i.e. $HOME/Pictures/)

-Go to $HOME/Pictures/ folder

-Give executable right to you script:
chmod u+x share_files.py

-Launch the script
./share_files.py

-Go to http://localhost:8000/
You should see the list of files you have under $HOME/Pictures/

To have the folder accessible for others, give away your IP address instead of localhost

Note:
The above script was found here

No comments: