Create an Instant Web Server via Terminal Command Line and Python
Want to quickly share a file, test some code, or broadcast something? You can instantly create a web server out of the current directory by using nothing but python, yup, no apache, no nginx, no litespeed, all python, which ships with pretty much every unix variation these days. The command is remarkably simple considering how useful it is, and test it out yourself open up a Terminal window and any web browser.
This trick starts a simple web server instantly with nothing but python, it works in Mac OS X, Linux, FreeBSD, and any other unix platform that has python.
How to Start an Instant Web Server with Python
To create the instant web server from the command line, type the following in whichever directory you wish to have accessible via browsers and HTTP:
Start a Simple Web Server in Python 2
python -m SimpleHTTPServer
Start Simple Web Server in Python 3
python -m http.server
This will publish the current directory as a web server immediately, so if you have an index.html file that will immediately be displayed, otherwise it will just list the directory contents at either your localhost IP or “0.0.0.0”. Note that port 8000 is the default port setting for this feature, meaning to access the web server from a browser one would enter the following address: http://0.0.0.0:8000
Remember, without an index.htm or index.html file in the directory, a simple directory listing of the CWD/PWD would show up to the browser instead.
Once running, the terminal will update as pages and data are loaded from the python web server, showing standard http logging information like GET and PUSH requests, which files are being accessed and by whom, 404 errors, IP addresses, dates, times, and all that you would expect from a standard http log as if you were tailing an apache access log file.
You can get fancier if you’d like and specify a port, giving some level of obscurity to the web server if you’re hoping to only broadcast something to a specific individual, or if you just don’t want it showing up on a standard 8000 portscan. All you need to do is specify a port number at the end of the aforementioned command, like so:
python -m SimpleHTTPServer 4104
This would make the web server launch in the current directory at the IP with port 4104, for an example let’s assume the localhost IP: http://127.0.0.1:4104, or http://0.0.0.0:4104 depending on your settings.
This is a very handy tip if you’re doing some quick web development and want to immediately check it in a browser or show it to someone else, and you don’t want to take the time to sftp or commit it to a repository. This should work on any unix variant OS, including FreeBSD, Linux, Ubuntu, Redhat, and of course Mac OS X is included as well.
The video below demonstrates a python server being launched in a directory with a simple index.html file, it also shows how the running python command reflects what is going on with the http connection like a normal http server log would:
Let us know if you find any particularly worthwhile uses or additional secrets for this great little trick.