How to Create Webserver- Python 'SimpleHTTPServer'

To Create Webserver- Python ‘ SimpleHTTPServer’

SimpleHTTPServer is a python module that allows you to create a web server or serve your files in a snap instantly. Main benefit of python’ s SimpleHTTPServer is, you don’ t require to install anything if you have python interpreter already installed. Use of Python ‘ SimpleHTTPServer’ to Create Webserver or Serve Files Instantly is explained in this article.

Checking for Python Installation

By issuing the below command, check whether python is installed in your server.

root@linuxhelp:~# python --version
Python 2.7.11+


Install python using yum or apt-get, if using operating systems like RHEL, CentOS, Debian, Ubuntu or other Linux operating systems.

To Create a Test Directory and Enable SimpleHTTPServer

Now let us create a directory for http web access.

root@linuxhelp:/test# mkdir linux
root@linuxhelp:/test# cd linux/
root@linuxhelp:/test/linux# mkdir dir1 dir2
root@linuxhelp:/test/linux# touch file{1..10}
root@linuxhelp:/test/linux# ls
dir1  file1   file2  file4  file6  file8
dir2  file10  file3  file5  file7  file9
root@linuxhelp:/test/linux# cd ..


Try python’ s SimpleHTTP Server module by running below command within test directory.

root@linuxhelp:/test# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

After enabling SimpleHTTPServer, it will automatically start serving files via port number 8000. Now open up a web browser and type " ip_address:port_number" .


Linux client

Windows client

Click on link ' linux' to browse files and directories in the linux directory.

SimpleHTTPServer serves files successfully. After access your server via browser, look at the terminal where you have executed your command.

root@linuxhelp:/test# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.5.183 - - [14/May/2016 14:15:24] " GET / HTTP/1.1"  200 -
192.168.5.183 - - [14/May/2016 14:15:25] code 404, message File not found
192.168.5.183 - - [14/May/2016 14:15:25] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.183 - - [14/May/2016 14:15:25] code 404, message File not found
192.168.5.183 - - [14/May/2016 14:15:25] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.183 - - [14/May/2016 14:18:06] " GET / HTTP/1.1"  200 -
192.168.5.183 - - [14/May/2016 14:18:21] " GET /linux/ HTTP/1.1"  200 -
192.168.5.11 - - [14/May/2016 14:20:41] " GET / HTTP/1.1"  200 -
192.168.5.11 - - [14/May/2016 14:20:41] code 404, message File not found
192.168.5.11 - - [14/May/2016 14:20:41] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.11 - - [14/May/2016 14:20:41] code 404, message File not found
192.168.5.11 - - [14/May/2016 14:20:41] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.11 - - [14/May/2016 14:20:48] " GET /linux/ HTTP/1.1"  200 -

To Change SimpleHTTPServer Port

Python’ s SimpleHTTPServer serves files and directories via port 8000 by default, to define a different port number use the following command.

root@linuxhelp:/test# python -m SimpleHTTPServer 9999
Serving HTTP on 0.0.0.0 port 9999 ...
192.168.5.183 - - [14/May/2016 14:30:18] " GET / HTTP/1.1"  200 -
192.168.5.183 - - [14/May/2016 14:30:18] code 404, message File not found
192.168.5.183 - - [14/May/2016 14:30:18] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.183 - - [14/May/2016 14:30:18] code 404, message File not found
192.168.5.183 - - [14/May/2016 14:30:18] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.183 - - [14/May/2016 14:30:53] " GET /linux/ HTTP/1.1"  200 -
192.168.5.11 - - [14/May/2016 14:31:31] " GET / HTTP/1.1"  200 -
192.168.5.11 - - [14/May/2016 14:31:31] code 404, message File not found
192.168.5.11 - - [14/May/2016 14:31:31] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.11 - - [14/May/2016 14:31:31] code 404, message File not found
192.168.5.11 - - [14/May/2016 14:31:31] " GET /favicon.ico HTTP/1.1"  404 -
192.168.5.11 - - [14/May/2016 14:31:42] " GET /linux/ HTTP/1.1"  200 -
192.168.5.11 - - [14/May/2016 14:31:49] " GET /linux/ HTTP/1.1"  200 -


Linux client

Windows client

To Serve Files from Different Location

To serve files in a specific location without going to the path, use the following command.


root@linuxhelp:/home# pushd /test/linux/  python -m SimpleHTTPServer 9999  popd /test/linux /home
Serving HTTP on 0.0.0.0 port 9999 ...


Linux client

Windows client

To Serve HTML Files

Open index.html file in your serving location, and add the following entries.

< html> 
< head> < title>  Welcome to Linuxhelp.com < /title> 
< /head> 
< body> 
< h5>  Welcome To Linuxhelp.com < /h5> 
< h4>  Welcome To Linuxhelp.com < /h4> 
< h3>  Welcome To Linuxhelp.com < /h3
< /body> 
< /html> 


Save the file and run the SimpleHTTPServer.

root@linuxhelp:/home# cd /test/linux/
root@linuxhelp:~# pushd /test/linux/  python -m SimpleHTTPServer 9999  popd 
/test/linux ~
Serving HTTP on 0.0.0.0 port 9999 ...


Linux client

Windows client

Tag : Python
FAQ
Q
What is the option for extracting timecodes in Flvextract?
A
option for extracting timecodes in Flvextract is "-tExtract timecodes."
Q
What was its official website to look for updates of flvextract-python
A
The official website for flvextract-python is http://www.moitah.net/.
Q
What was its essential property of FlevExtract?
A
Flvextract is an extracting tool that extracts the audio and video files from FLV files and can also remux to MP4 or MKV without converting it
Q
Is it possible to access the SimpleHTTPServer from outside the LAN, like over DNS?
A
As far as you’ve static IP address, you can access SimpleHTTPServer from anywhere in the world using IP address and port
Q
do you know how to keep it running in the background? Whenever I log out of the ssh session, it cannot server again and gives 404 error!!
A
Just add & string at the end of the command to keep the process running in background even after you logout from your SSH session. For example, # python –m SimpleHTTPServer &