How To Run One Python Command To Turn Your Computer Into A Web Server

I don’t know if you have encountered such a situation, that is, sometimes you want to transfer some files from the computer to your mobile phone or pad. You either need to connect to the computer with a data cable, sometimes you need to install various drivers to carry out data transmission, or you need to use a third-party tool to transmit on the lan.

1. Start Http Web Server On Computer.

Today i’d like to share a skill with you. With one python command, you can turn your computer into a web server, so that your mobile phone/pad can directly access the files stored on your computer. This command is python3 -m http.server.

$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [22/Sep/2020 13:31:07] "GET / HTTP/1.1" 200 -

When you finish execute above command, your computer will start a web server that listen on port 8000. Then your mobile phone or pad can access all the files saved on your computer ( the mobile phone/pad and the computer should locate in same local network ).

If you want to share another folder on your local computer, you should run command python3 -m http.serverunder that folder path.

2. Access Above Http Web Server Through Mobile Phone Or Pad.

First you should get your local computer’s ip address with command ifconfig.

$ ifconfig
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=400<CHANNEL_IO>
    ether 4c:8d:79:e1:bc:e2 
    inet6 fe80::76:20fc:9e37:94fd%en1 prefixlen 64 secured scopeid 0x8 
    inet 192.168.31.31 netmask 0xffffff00 broadcast 192.168.31.255
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect
    status: active

Then you can open a web browser on your mobile phone or pad, then input url http://your_local_machine_ip:8000. Now you can see all the files listed in the page. You can download them as you need.

3. Start Python Http Web Server On Other Port.

If you want to start the http web server with other port number than default port number 8000, you can run command python3 -m http.server port_number to start it.

python3 -m http.server 9000