How To Setup a Simple Web Server in Python
Simple is better than complex.
Yesterday, I would like to review some files while I was taking a bath. 🛀 Unfortunately, these files were in my Mac Mini instead of my mobile phone. To make matters worse, I didn’t have an iPhone. (Yes, I am using an M1 Mac Mini but an Android phone.)
The AirDrop, as we all know, is a super convenient application to transfer data between iPhone and Mac. But it doesn’t support Android phones and it’s maybe too late to buy a new iPhone before I started taking a bath yesterday.
Of course, there may be many third-party tools that can help. After googling them, I found there are too many choices in this case. Because of my decidophobia, it’s stressing me out.
So, as a Python expert (self-defined), I was wondering if Python can help me.
After thinking for a while, I got it! All I needed was just one line of Python code:
python3 -m http.serverThe above code can start a very simple Web server serving files relative to the current directory, and the default port of it is 8000. Then, I just needed to open the 192.168.x.xx:8000 with my Android phone to check out the files directly and seamlessly. (By the way, 192.168.x.xx stands for my Mac Mini’s IP address.)
Simple and cool, isn’t it? 🙂
More Details
Mind the versions of Python
This is literally a built-in functionality of Python. If you are using Python 3, what you need to input on the terminal is just:
python3 -m http.serverBut if you are still using Python 2, the syntax is a bit different:
python -m SimpleHTTPServerHow to change the port
Simple as always. Just add the expected port at the end:
python3 -m http.server 8080Or Python 2:
python -m SimpleHTTPServer 8080How to customise the Webserver
The above is to setup a very basic server serving files at current directory. Is that possible to create a Web server which can response HTTP requests and return HTML websites as this simple?
Yes, everything in Python is simple:




