Quick Python Web Server

Have you ever wanted to quickly run a web server, but didn’t feel like having to install and configure something like apache, or nginx?

Python to the rescue!  You can start a quick web server with one line of code.

Python 3:

python3 -m http.server <optional_portport>

Python 2:

python2 -m SimpleHTTPServer <optional_port>

Accessing the web server in your browser looks like this:

You can pass an optional port to the end of this code, otherwise, this python web server will

default to port 8000.  You will also see all of the logs and error messages in standard out on your terminal (you could of course redirect this to a log file for safe keeping).  

This quick web server is not intended to be for production use, but it can come in very handy for 

testing, or for transferring files from one system to another.  

Examples:

Testing a quick webpage

mkdir ~/quickwebsite/

echo "You found me" > ~/quickwebsite/index.html

python3:

python3 -m http.server --directory ~/quickwebsite/

python2:

cd ~/quickwebsite/

python2 -m SimpleHTTPServer

If you have something already running on port 8000, change the port, and start your python server:

python3 -m http.server 12345

File Transfer

Need to transfer a file from one system to another, but most standard protocols are blocked, like 

ssh, ftp?  Guess what’s not usually blocked!  HTTP is typically wide open on networks, especially if you choose standard ports like 80, or even Ephemeral Ports .

NOTE: you will need sudo or run your simple web server as root to use a port under 1024 (like port 80)

But if you throw up a quick web server, you can easily grab the file you need from a remote computer:

python2 -m SimpleHTTPServer

and on your remote computer, open a browser and use the IP address of the server running your web server, followed by the port.

Things to remember:

Once again, this is not intended for production use, but it can be a lifesaver when you are dealing with a network that has  a ton of restrictions and blocked ports.  

Also, remember to tear down this webserver when you are done.  Simply use Control + C to break the web server session.

This is using HTTP, not HTTPS, so everything you are doing is passed in the clear, meaning if anyone is sniffing the traffic on your network, they can see everything you are doing.

Leave a Reply

Your email address will not be published. Required fields are marked *