Top
Posted: June 29, 2011

Simple Python Server Script

Here is a little python script that starts up a web server in the directory where the script is run. This is not my script, though I did add a couple optional arguments.

While the script is running, the directory from which the script was run will be web-accessible at your ip address, at the default port 8080 (e.g. 192.168.0.100:8080). If you want to broadcast beyond your local network, just make sure that your router is forwarding the correct ports to your computer.

The script produces an index page equipped with an upload button, making it very handy for quick file-sharing via http.

(Please note: an even better solution can be found using node, such as here.)

To install the script so that it can be called from any directory, go to Terminal (in mac), and find the directory where the script is located (using cd). Assuming the script is called ‘server.py’, run the following commands:

sudo cat server.py > /usr/local/bin/server

sudo chmod +x  /usr/local/bin/server

To uninstall, go to Terminal and type:

sudo rm /usr/local/bin/server

To run the script, go in Terminal to the directory where want to start up the server, and type:

server

I added two optional command line arguments, ‘-p’ and ‘-s’. Use ‘-p’ to change the port (default is 8080), and ‘-s’ to show hidden dot files. Example usage:

server -p 8000 -s