Creating a Simple HTTP Server with one line of Python

Python can run a simple HTTP server, using a built-in module called SimpleHTTPServer this provides standard GET and HEAD request handlers.

The great thing about this is that you do not have to configure anything, you only have to have Python installed. This is perfect to use when you need a quick web server and don’t want to mess with setting up apache. You can use this to turn any directory on your system into a web server and comes in very handy if you need to get some files on to a remote server.

How To Setup Simple HTTP Server

Start by navigating to the directory that you want to share

$ cd /home/somedir

Then just type..

$ python -m SimpleHTTPServer

This will start the simple HTTP server serving you all the files and directories in your current working directory on a default port of 8000.

You can check this by typing http:// then the IP address of the server in any browser with a port of 8000 on the end.

You can also change the port to something else by adding a port number on the end of the command.

$ python -m SimpleHTTPServer 1337

Again this can be checked from any browser making sure you add the same port number you used in the command.

Python 3

All the previous commands where for Python 2, but don’t fear Python 3 still has a built-in module, it just uses http.server instead of SimpleHTTPServer like below.

$ python3 -m http.server 8000

Serve up a Web page

You don’t have to just show directories and files. if you add an index.html to the directory you’re sharing, the simple HTTP server will serve this page up instead.

see the banner grab using Netcat.

nc 127.0.0.1 1337
GET / HTTP/1.1

HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/2.7.6
Date: Fri, 30 Mar 2018 13:56:03 GMT
Content-type: text/html
Content-Length: 92
Last-Modified: Fri, 30 Mar 2018 13:14:19 GMT

<head>
</head>
<body>

<H1>Hemp's Tutorials</h1>
<H3>Simple HTTP Server</H3>
</body>

Logging in the Terminal

As long as the HTTP server is running the terminal will update as data is loaded from the Simple HTTP Server. You should see standard HTTP logging information ( GET and PUSH), 404 errors, IP addresses, dates, times and all that you would expect from a standard HTTP log as if you were looking through any apache access log.

Serving HTTP on 0.0.0.0 port 1337 ...
127.0.0.1 - - [30/Mar/2018 14:50:32] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Mar/2018 14:50:34] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Mar/2018 14:51:07] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Mar/2018 14:51:11] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Mar/2018 14:52:58] "GET / HTTP/1.1" 200 -
192.168.10.5 - - [30/Mar/2018 14:54:48] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Mar/2018 14:56:03] "GET / HTTP/1.1" 200 -

Leave a Reply

Your email address will not be published. Required fields are marked *