Python Simple HTTP WebServer

Python has got a very useful inbuilt web server which can be used to:
  • Quickly test a web page.
  • Host files which can be transferred to other computer very easily.
  • Act as a host web server which can be used maliciously if we can redirect traffic from target computer to hosting server.
  • Many more
Listed below are the various options which we can use:

Python3:
Using Command Line:
=================
/* Defaults:Port 8000,binds all interfaces,current directory */
python3 -m http.server

/* Use port 1000,binds all interfaces,current directory */
python3 -m http.server 1000

/* Use port 1000,only binds to local-host,current directory */
python3 -m http.server 1000 --bind 127.0.0.1

/* Use port 1000,binds all interfaces,use tmp directory */
python3 -m http.server 1000 --bind 127.0.0.1--directory /tmp/

Using Code:
=========
import http.server
import socketserver

PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Serving At Localhost PORT", PORT)
    httpd.serve_forever()

Python2:
Using Command Line:
=================
python -m SimpleHTTPServer 8000

Using Code:
=========
import SimpleHTTPServer
import SocketServer

PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()

References:
=========
https://docs.python.org/2/library/simplehttpserver.html

















Comments

Popular Posts