Aug 12, 2019

How to use python as a http server

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. :-)

Mar 08
How stack works in function call

The stack is an important concept in computer science. If you are planning to learn reverse....

Jun 15
SQL injection login bypass

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

Aug 12
Error Handling | Python Programming

Errors are absolutely normal when programming. You may get hundred of errors while learning. Not....

Replying to 's comment Cancel reply
ABOUT AUTHOR
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.

CATEGORIES
SOCIAL
RANDOM ARTICLES