avatarYang Zhou

Summary

The article provides a guide on setting up a simple web server using Python to access files from a mobile device.

Abstract

The author shares a personal anecdote about needing to access files on a Mac Mini from an Android phone while in the bath, without the convenience of AirDrop. The solution presented is the use of Python's built-in HTTP server module, which allows for the creation of a simple web server with a single line of code. The article details how to start the server, access files via a mobile browser, and customize the server settings, including changing the port number. While the Python HTTP server is praised for its simplicity and utility in specific scenarios, such as local testing, the author emphasizes that it is not suitable for production environments due to limited security features.

Opinions

  • The author values simplicity and convenience, choosing a Python-based solution over numerous third-party tools.
  • There is an expressed preference for Python over other solutions, as the author identifies as a Python expert and opts for Python's built-in functionality.
  • The author appreciates the flexibility of Python's HTTP server, noting its ability to serve HTML websites in addition to files.
  • A clear warning is given against using Python's http.server for production purposes, citing its basic security checks and recommending it only for simple use cases.
  • The author encourages readers to follow them and become Medium members to access more content on programming and technology.

How To Setup a Simple Web Server in Python

Simple is better than complex.

Photo by Hal Gatewood on Unsplash

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.server

The 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.server

But if you are still using Python 2, the syntax is a bit different:

python -m SimpleHTTPServer

How to change the port

Simple as always. Just add the expected port at the end:

python3 -m http.server 8080

Or Python 2:

python -m SimpleHTTPServer 8080

How 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:

The above program built a Web server that can handle HTTP GET requests and send back an HTML file as a response. As we can see, Python provides all the fundamental APIs for server development, we just need to develop our production logic based on them.

Do not use this for production

In fact, there is a statement on Python’s official document:

Warning: http.server is not recommended for production. It only implements basic security checks.

Therefore, this http.server module is only suitable for simple using cases, such as local testing or checking out files on your Mac when you are taking a bath.

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more interesting stories about programming and technologies!

Python
Programming
Web Development
Technology
Software Development
Recommended from ReadMedium