Use Python SimpleHTTPServer to Quickly Serve Filesystem Resources

 Python SimpleHTTPServer represents a fast alternative to serve files from a directory on your system via HTTP protocol without the need to install Apache, Nginx or other similar HTTP servers on your system. The main advantage of using Python SimpleHTTPServer is that you don’t have to install anything on your Linux system as long as you have the Python interpreter installed on your machine. Most of the Linux distribution have Python interpreter installed by default and the SimpleHTTPServer is nothing more than a integrated Python module.
This tutorial will present a short Bash script that will help you to set and use the Python SimpleHTTPServer in order to quickly serve different filesystem resources and access files from directories via custom HTTP ports. The name of the Bash script that will be used as a command to manipulate the SimpleHTTPServer will be http-server and will provide three arguments that can be passed to the script: startstop and status.


STEP ONE – Create http-server Bash Command to Handle SimpleHTTPServer
1.      The first thing that you should take into consideration is to create http-server bash script on a system executable path (defined by $PATH environment variable), preferably on a local executable path in order not to mess with system commands. For the purpose of demonstration I will use the port 8081 for SimpleHTPPServer in order to access all the files that reside on /var/log/ path via HTTP protocol.
So, let’s start creating the http-server Bash script on a local executable path with nano editor by issuing the following command:
sudo nano /usr/local/bin/http-server

2.       Once the file is opened for editing add the below content (use copy-paste):


  1. #!/bin/bash



  2. WWW_PORT='8081'

  3. WWW_PATH="/var/log/"



  4.   case $1 in

  5.         start)

  6.         cd $WWW_PATH

  7. nohup python -m SimpleHTTPServer $WWW_PORT >> /tmp/nohup.log 2>&1 &

  8. sleep 2

  9. stat=`netstat -tlpn | grep $WWW_PORT | grep "python" | cut -d":" -f2 | cut -d" " -f1`

  10.         if [[ $WWW_PORT -eq $stat ]]; then

  11. sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`

  12. echo -e "Server is running:\n$sock"

  13.         else

  14. echo -e "Server is stoped"

  15.         fi

  16.                 ;;



  17.         stop)

  18. pid=`ps -ax | grep "[S]impleHTTPServer $WWW_PORT"| awk '{print $1}'`

  19.         kill -9 $pid 2>/dev/null

  20. rm -f /tmp/nohup.log

  21. stat=`netstat -tlpn | grep $WWW_PORT | grep "python"| cut -d":" -f2 | cut -d" " -f1`

  22.         if [[ $WWW_PORT -eq $stat ]]; then

  23. sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`

  24. echo -e "Server is  still running:\n$sock"

  25.         else

  26. echo -e "Server is stoped"

  27.         fi



  28.                 ;;



  29.         status)

  30. stat=`netstat -tlpn |grep $WWW_PORT| grep "python" | cut -d":" -f2 | cut -d" " -f1`

  31.         if [[ $WWW_PORT -eq $stat ]]; then

  32. sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`

  33. echo -e "Server is running:\n$sock"

  34.         else

  35. echo -e "Server is stoped"

  36.         fi

  37.                 ;;

  38.         *)

  39. echo "Use $0 start|stop|status"

  40.                 ;;

  41. esac



Bash Command for SimpleHTTPServer
Bash Command for SimpleHTTPServer
3.       After you finish editing the file, save it (Ctrl+o) and close it (Ctrl+x), then make sure the file has execution permissions by issuing the following command:
sudo chmod +x /usr/local/bin/http-server

4.      Finally, start and verify the status Python SimpleHTTPServer by issuing the below commands:
sudo http-server start
sudo http
-server status

start and verify SimpleHTTPServer
start and verify SimpleHTTPServer

5.       To visit the server webpage open a browser and use the server’s IP Address and Port combination as below (replace IP Address accordingly):
http://192.168.1.210:8081
vist SimpleHTTPServer directory listing
vist SimpleHTTPServer directory listing

As you can see the SimpleHTTPServer doesn’t serve a webpage by default. It only creates a directory list of the path you supplied via the WWW_PATH variable, which in this case is /var/log/. You can now access and download all your log files via HTTP in case you want to do some system troubleshooting.

6.      The http-server script configures the SimpleHTTPServer to write the log file at /tmp/nohup.log. In case you want to view SimpleHTTPServer log file issue the following command:
cat /tmp/nohup.log

view SimpleHTTPServer log file
view SimpleHTTPServer log file

For real-time log file view run the following command:
tailf /tmp/nohup.log

7.       For stopping the SimpleHTTPServer just issue the following command:
sudo http-server stop

stop SimpleHTTPServer
stop SimpleHTTPServer


STEP TWO – Change SimpleHTTPServer Port and Webroot Path and Serve HTML Static Files
8.       Changing  the Port and Path directives for SimpleHTTPServer is pretty simple. Just open http-server file for editing and change the WWW_PORT and WWW_PATH header  variables with your own. By default SimpleHTTPServer can parse only HTML static files and automatically serve index.html files.
So, for the sake of demonstration, open /usr/local/bin/http-server file for editing and change the WWW_PATHvariable path to serve HTML static files from /srv/ system path as illustrated in the below screenshot.
serve files from /srv path
serve files from /srv path

9.       Then create a simple index HTML file on /srv/ path by issuing the following command:
sudo echo "<html><strong> Simple Pyhton HTTP Server</strong></html>" | sudo tee -a /srv/index.html

10.       In order to visit the default webpage served by SimpleHTTPServer, start the http-server and direct your browser at your machine IP:Port URL
sudo http-server start

http://192.168.1.210:8081
SimpleHTTPServer default webpage
SimpleHTTPServer default webpage


STEP THREE – Enable http-server Sistem-Wide
11.      In order to automatically start SimpleHTTPServer after every system reboot, open /etc/rc.local file with rootprivileges and add the following file before the last exit 0 statement:
sudo nano /etc/rc.local


  1. /usr/local/bin/http-server start



Enable http-server system-wide
enable http-server system-wide

12.      To assure that the rc.local script is enabled system-wide on Debian-based distributions, install the sysv-rc-conf package, then issue the following command:
sudo sysv-rc-conf rc.local on

Using the http-server Bash script to handle Python SimpleHTTPServer can provide a simple and fast way through which you can turn any system directory into a web server directory and access almost every filesystem resource via HTTP without the need to install extra software on your system.
Previous
Next Post »

thankyou ConversionConversion EmoticonEmoticon