Python provides different HTTP and related modules in builtin and 3rd party modules. Python also provides some basic HTTP server modules native. In this tutorial we will learn how to run HTTP server in Python2 and Python3.
SimpleHTTPServer In Python2 From Commandline
We will use SimpleHTTPServer
module for Python2. We will just provide the module name the port number we want to run HTTP server from commandline. In this example we will run from 8000
.
$ python2 -m SimpleHTTPServer 8000

This screenshot means web server is listening from all network interfaces for TCP port 8000 for our HTTP web server.
SimpleHTTPServer In Python2 As Code
More complete way to run a HTTP server is running a web server script. We will use following code which is named webserver.py
.
import SimpleHTTPServer import SocketServer PORT = 8000 Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", PORT), Handler) print "serving at port", PORT httpd.serve_forever()
AND run like below.
$ python2 webserver.py

SimpleHTTPServer In Python3 From Commandline
As Python version 3 the name of the HTTP server is changed to the http.server . So we need to run following command from command line.
$ python3 -m http.server 8000

We can see from output that all network interfaces are listening port 8000 with HTTP protocol.