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

Aug 12
Linux directory managing

As a Linux user you must master Linux terminal. You should be able to handle files and directories....

Jun 09
Functions in python programming

Functions are life savers. Yes they make our life easier. A function is a peace of code and used to....

Aug 12
Modules | Python programming

Module is a simple but powerful concept in python. We saw in C programs we used header files. (....

Replying to 's comment Cancel reply