DEV Community

chinmay chhajed
chinmay chhajed

Posted on

Transfer files between your phone and computer without any cable and internet!

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

  • 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
  • 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

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

Top comments (4)

Collapse
 
brandonwallace profile image
brandon_wallace

Hey Chinmay,
I turned this idea into a small program. It gets the IP address automatically. Run it like this

$ python3 server_files.py

OR like this

$ python3 server_files.py --host '192.168.12.34' --port 1234

#!/usr/bin/env python3
'''
Server files from current directory.
Requirements: Python3.4+
'''

import socket
import argparse
import http.server
import socketserver


parser = argparse.ArgumentParser(description='Serve files from the current \
                                 directory.')

host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)

parser.add_argument('--host', default=ip, type=str, required=False,
                    help='Specify the ip address to listen on.')

parser.add_argument('--port', default=8080, type=int, required=False,
                    help='Specify the port to listen on.')

args = parser.parse_args()

handler = http.server.SimpleHTTPRequestHandler


with socketserver.TCPServer((args.host, args.port), handler) as httpd:
    print(f'Server is listening on {args.host} on port {args.port}.')
    httpd.serve_forever()
Enter fullscreen mode Exit fullscreen mode

github.com/brandon-wallace/serve_f...

Collapse
 
crestiancik profile image
Crestiancik • Edited

Wait, so is that actually possible?! It sounds like something incredible! I think this kind of small program should be in demand as a hell of a lot of people are likely to transfer some files from computer to their phone or vice versa without those stupid cables, but sometimes it might happen that you might not have internet, and here comes this program. Well, I would be using this program at least once per day, as my cox wifi is terrible. I have to keep this guide xtrium.com/cox-panoramic-wifi-not-... as wallpaper on my pc, so I know how to repair the damn router.

Collapse
 
haiyong profile image
Haiyong

It looks great, I will try it when I have time

Collapse
 
brandonwallace profile image
brandon_wallace

Noice! I like this idea.