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: start
, stop
and status
.STEP ONE – Create
http-server
Bash Command to Handle SimpleHTTPServer1. 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):
- #!/bin/bash
- WWW_PORT='8081'
- WWW_PATH="/var/log/"
- case $1 in
- start)
- cd $WWW_PATH
- nohup python -m SimpleHTTPServer $WWW_PORT >> /tmp/nohup.log 2>&1 &
- sleep 2
- stat=`netstat -tlpn | grep $WWW_PORT | grep "python" | cut -d":" -f2 | cut -d" " -f1`
- if [[ $WWW_PORT -eq $stat ]]; then
- sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`
- echo -e "Server is running:\n$sock"
- else
- echo -e "Server is stoped"
- fi
- ;;
- stop)
- pid=`ps -ax | grep "[S]impleHTTPServer $WWW_PORT"| awk '{print $1}'`
- kill -9 $pid 2>/dev/null
- rm -f /tmp/nohup.log
- stat=`netstat -tlpn | grep $WWW_PORT | grep "python"| cut -d":" -f2 | cut -d" " -f1`
- if [[ $WWW_PORT -eq $stat ]]; then
- sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`
- echo -e "Server is still running:\n$sock"
- else
- echo -e "Server is stoped"
- fi
- ;;
- status)
- stat=`netstat -tlpn |grep $WWW_PORT| grep "python" | cut -d":" -f2 | cut -d" " -f1`
- if [[ $WWW_PORT -eq $stat ]]; then
- sock=`netstat -tlpn | grep $WWW_PORT | grep "python"`
- echo -e "Server is running:\n$sock"
- else
- echo -e "Server is stoped"
- fi
- ;;
- *)
- echo "Use $0 start|stop|status"
- ;;
- esac
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
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
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
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
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_PATH
variable path to serve HTML static files from /srv/
system path as illustrated in the below screenshot.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
URLsudo http-server start
http://192.168.1.210:8081
STEP THREE – Enable
http-server
Sistem-Wide11. In order to automatically start SimpleHTTPServer after every system reboot, open
/etc/rc.local
file with root
privileges and add the following file before the last exit 0
statement:sudo nano /etc/rc.local
- /usr/local/bin/http-server start
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.
thankyou ConversionConversion EmoticonEmoticon