How to use python as a http server

HacksLand | The computer science playground

Posted by Thilan Dissanayaka on Aug 12, 2019
Hello guys, Today I'm hear with another quick tutorial. in this one I'll explain you how we can use python as a http web server. Yes we have apache2 and nginx. But for some quick and short works this tool is so handy. We can build a simple server wiyhing a single line of python code. Let's see how. First i'm going to show you how we can use it in python 2.x version. Hear we have a module called SimpleHTTPServer. The syntax for use it is python -m SimpleHTTPServer [port] The default port is 8000. So if you don't specify a port it will be used. Let's serve on port 4444.
python -m SimpleHTTPServer 4444
Now we can browse it through our browser. If you visiting it in same host you have to go 127.0.0.1:4444. If you have added an index.html file it will be displayed. If not you can see the directory listing of current folder.   python http server Awesome ! . it's working fine.  Do you want to see above html source too? why can't. :-)
<!DOCTYPE html><html><head><style type="text/css">.heading{text-align: center;color: lime;font-size: 72px;}.sub-heading{text-align: center;color: white;font-size: 40px;}.hl{text-align: center;font-size: 96px;color: white;}.hl-up{color: red;text-align: center;font-size: 96px;}a{text-decoration: none;}body{background: black;}</style><title>Blog of Thilan</title></head><body><h1 class="heading"> Welcome to </h1><a href="http://hacksland.net"><h1 class="hl"><span class="hl-up">H</span>acks<span class="hl-up">L</span>and</h1></a></body></html>
Just for fun. :-p When sombody visit the server , HTTP requests will be logged on the terminal. Also this server will serve until you close the shell.  
[email protected]:~$ python -m SimpleHTTPServer 4444
Serving HTTP on 0.0.0.0 port 4444 ...
127.0.0.1 - - [26/Jun/2019 18:05:15] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Jun/2019 18:06:37] code 404, message File not found
127.0.0.1 - - [26/Jun/2019 18:06:37] "GET /any.html HTTP/1.1" 404 -
Now we can use it on python 3.x versions too. Hear we have to use http module instead of simpleHttpServer. The syntax is same.
python3 -m http.server 8000
So guys it's all for this tutorial. Yes it was a short and quick one. But we can use this trick in many times. In futurre tutorials I'll show you how we can use this in ctf , and web applicatio pentesting.Thank for reading. :-)
Hi, I'm Thilan. An engineering student from SriLanka. I love to code with Python, JavaScript PHP and C.

Also read

Feb 18
Moving data with assembly

In normal Windows/Linux environment you have heard about moving data or files. What we do is....

Aug 12
Format strings | Python programming

Format strings are used to format the printing output. We can print a string with variables in it.....

Aug 12
IP Address Explained

In this tutorial I'm going to cover an important topic in networking. Yes it's internet protocol.....

Comments