Transfer files between your phone and computer without any cable and internet!
Tags: productivity, python, lifehack, wifi
Published: 2021-02-22 08:53:35

Isn't it tedious to always reach out for a USB cable and then connect phone with computer, wait while computer detects, then browse for files and then finally transfer! What a pain to do so many steps.

Worry not, here is a _wireless_ solution to copy data from one device to other. All you need is Python and a supporting mobile app!

To do this, we will be using a simple concept: HTTP Server and Client. A server will be source of files and a client will be the one copying files from server.

Since we will be transferring files wireless, we need to connect mobile and computer to a common WiFi router. If you don't have a router, turn hotspot on from your mobile and connect your computer to it.

## Transfer files from computer to phone

- Make sure you have python installed. If not then first [install python](https://www.python.org/downloads/).
- Next step is to check version of python. Open a terminal (or command prompt on Windows) and run `python --version`
- If you have a python version starting from `3.x`, then run the command `python -m http.server` in terminal.
- If your python version is `2.x` then run `python -m SimpleHTTPServer`

> Output should look something like this in both the cases:
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1uo9k3z22pgok5ghwip.png)
 
- Next get the private IP address of your machine.
    - For Windows, open command prompt and run `ipconfig`. Your local IP address will be listed under ‘IPv4 Address’.
    - For Linux and Mac, open terminal and run command `ifconfig`. In the resulting information spewed out, we are looking for the section starting with `wl`. It could be `wlp3s0` or something else. Basically it denotes wireless lan. Under this section, we need address after `inet`. In my case, it's `192.168.10.11`.
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46kajsrj1f41kxobo12h.png)

- Once we have the IP address, go to the browser in your phone and enter that address with a `:8000` at the end in address bar. This `8000` comes from the output of `python -m http.server` command. As you can see in above image, default port is `8000`. For my case, I would enter `192.168.10.11:8000` in my mobile browser and go to the link. Once the page loads, congrats, you may navigate to the file and download it.

- If page doesn't load in mobile browser, make sure that you do not have a `https://` at start of URL. If present then remove it.

Mobile browser for my case:
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03rqfk30jsn0k2nht76m.png)


> Bonus: If you need a specific file, then in terminal navigate to the directory where file is present and run the python server command in that directory. Python server will start from which ever directory command is executed.

## Transfer files from phone to computer

- For this you will need a mobile app to run HTTP Server like [this](https://play.google.com/store/apps/details?id=jp.ubi.common.http.server&hl=en&gl=US) or any other app. Just open the app and run the server. For this app, click on "off" to turn the server on.
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phe2sdnnbdosv06zxfur.png)
  - Open the link given on computer browser. From above screenshot, `192.168.10.2:12345/`. And done! Browser files present in your phone from your computer.