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 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=""><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 port 4444 ... - - [26/Jun/2019 18:05:15] "GET / HTTP/1.1" 200 - - - [26/Jun/2019 18:06:37] code 404, message File not found - - [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

Aug 12
SQL injection login bypass

SQL injection, The classical example of web application vulnerabilities. Actually the term SQL....

Oct 30
How to learn C

C is a classical programming language. Many of other programming languages are based on C. If you....

Sep 23
PUSH and POP with stack

This is the second tutorial of our stack tutorial set. Hear we are going to talk about some two....