How To Create Python SimpleHTTPServer on ubuntu 18.04

To Create Python SimpleHTTPServer on Ubuntu 18.04

SimpleHTTPServer is a python module that allows you to create a web server or serve your files in a snap instantly. The main benefit of python’ s SimpleHTTPServer is, you don’ t require to install anything if you have python interpreter already installed. This tutorial covers the method to create Python SimpleHTTPServer on Ubuntu 18.04.

Creating Python SimpleHTTPServer

Checking for Python Installation

By issuing the below command, you can check, whether python is installed on your server.

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

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

Create a Test Directory and Enable SimpleHTTPServer to create a directory for http web access. Create a directory which we want to access through web.

root@linuxhelp:~# cd /
root@linuxhelp:/# mkdir test
root@linuxhelp:/# cd test/
root@linuxhelp:/test# mkdir linux
root@linuxhelp:/test# cd linux/

After that, create some files to access through the http server.

root@linuxhelp:/test/linux# mkdir dir1 dir2
root@linuxhelp:/test/linux# touch file{1..9}
root@linuxhelp:/test/linux# ll
total 16
drwxr-xr-x 4 root root 4096 Apr 14 04:49 ./
drwxr-xr-x 3 root root 4096 Apr 14 04:48 ../
drwxr-xr-x 2 root root 4096 Apr 14 04:49 dir1/
drwxr-xr-x 2 root root 4096 Apr 14 04:49 dir2/
-rw-r--r-- 1 root root    0 Apr 14 04:49 file1
-rw-r--r-- 1 root root    0 Apr 14 04:49 file2
-rw-r--r-- 1 root root    0 Apr 14 04:49 file3
-rw-r--r-- 1 root root    0 Apr 14 04:49 file4
-rw-r--r-- 1 root root    0 Apr 14 04:49 file5
-rw-r--r-- 1 root root    0 Apr 14 04:49 file6
-rw-r--r-- 1 root root    0 Apr 14 04:49 file7
-rw-r--r-- 1 root root    0 Apr 14 04:49 file8
-rw-r--r-- 1 root root    0 Apr 14 04:49 file9

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

root@linuxhelp:/test/linux# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.7.232 - - [14/Apr/2018 04:50:59] " GET / HTTP/1.1"  200 -
192.168.7.232 - - [14/Apr/2018 04:51:01] code 404, message File not found
192.168.7.232 - - [14/Apr/2018 04:51:01] " GET /favicon.ico HTTP/1.1"  404 -
192.168.7.232 - - [14/Apr/2018 04:51:01] code 404, message File not found
192.168.7.232 - - [14/Apr/2018 04:51:01] " GET /favicon.ico HTTP/1.1"  404 &ndash 

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

Let' s go one step back and launch the http server from that location

root@linuxhelp:/test/linux# cd ..
root@linuxhelp:/test# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.7.232 - - [14/Apr/2018 04:51:46] " GET / HTTP/1.1"  200 -
192.168.7.232 - - [14/Apr/2018 04:51:49] " GET /linux/ HTTP/1.1"  200 -

Open up a web browser and type " http://< ip_address> :8000."

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.7.232 - - [14/Apr/2018 04:52:55] " GET / HTTP/1.1"  200 -
192.168.7.232 - - [14/Apr/2018 04:52:55] code 404, message File not found
192.168.7.232 - - [14/Apr/2018 04:52:55] " GET /favicon.ico HTTP/1.1"  404 -
192.168.7.232 - - [14/Apr/2018 04:52:55] code 404, message File not found
192.168.7.232 - - [14/Apr/2018 04:52:55] " GET /favicon.ico HTTP/1.1"  404 -
192.168.7.232 - - [14/Apr/2018 04:52:57] " GET /linux/ HTTP/1.1"  200 &ndash 

Open up a web browser and type " http://< ip_address> :9999"

To Serve Files from Different Location

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

root@linuxhelp:~# pwd
/home/user1
root@linuxhelp:~# cd /home/
root@linuxhelp:/home#
root@linuxhelp:/home# pushd /test/linux/  python -m SimpleHTTPServer 9999  popd /test/linux /home
/test/linux /home
Serving HTTP on 0.0.0.0 port 9999 ...
192.168.7.232 - - [14/Apr/2018 04:54:25] " GET / HTTP/1.1"  200 &ndash 

Now open up a web browser and type " http://< ip_address> :9999"

To Serve HTML Files

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

root@linuxhelp:/test/linux# vim index.html
root@linuxhelp:/test/linux# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.7.232 - - [14/Apr/2018 04:56:11] " GET / HTTP/1.1"  200 -
192.168.7.232 - - [14/Apr/2018 04:56:52] " GET / HTTP/1.1"  200 &ndash 

Open up a web browser and type " http://< ip_address> :8000"


With this, the method to Create Python SimpleHTTPServer on ubuntu 18.04 comes to an end.

FAQ
Q
How do I use it Python SimpleHTTPServer?
A
To start a HTTP server on port 8000 (which is the default port), simple type:

python -m SimpleHTTPServer [port]

This will now show the files and directories which are in the current working
directory.
Q
How to check server_version in python simplehttp server?
A
This will be "SimpleHTTP/" + __version__, where __version__ is defined at the module level.
Q
What is python flask?
A
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.
Q
What is WSGI in Python?
A
The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. The current version of WSGI, version 1.0.1, is specified in Python Enhancement Proposal (PEP) 3333.
Q
Any alternative for SimpleHTTp server
A
Apache HTTP server, Nginx, Lighttpd, Cherokee, etc.,