Simple Python HTTP server

Knocking up a simple and quick web server can be extremely useful without the need to install and configure a full fat web server. However, this isn't secure and will expose ALL files from the current directory executed in!

The official doc can be found here (v2) and here (v3).

NOTE: You will need to generate the certs for HTTPS version.

Python2:

HTTP

HTTPS

#!/usr/bin/env python2

import BaseHTTPServer, SimpleHTTPServer

httpd = BaseHTTPServer.HTTPServer(('<listen IP>', <port>),
        SimpleHTTPServer.SimpleHTTPRequestHandler)

httpd.serve_forever()

Or a simple one liner:

python -m SimpleHTTPServer
#!/usr/bin/env python2

import BaseHTTPServer, SimpleHTTPServer
import ssl

httpd = BaseHTTPServer.HTTPServer(('<listen IP>', <port>),
        SimpleHTTPServer.SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
        keyfile="key.pem",
        certfile='cert.pem', server_side=True)

httpd.serve_forever()

Python3:

HTTP

HTTPS

#!/usr/bin/env python3

import http.server, socketserver

Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(('<listen ip>', <port>), Handler)

httpd.serve_forever()

Or a simple one liner:

python -m http.server <port> --bind '<listen ip>'
#!/usr/bin/env python3

import http.server, socketserver
import ssl

Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(('<listen ip>', <port>), Handler)

httpd.socket = ssl.wrap_socket (httpd.socket,
        keyfile="key.pem",
        certfile='cert.pem', server_side=True)

httpd.serve_forever()