Search code examples
pythonmobile-phonesfile-transfer

Simple file transfer over wifi between computer and mobile phone using python


I'd like to be able to transfer files between my mobile phone and computer. The phone is a smartphone that can run python 2.5.4 and the computer is running windows xp (with python 2.5.4 and 3.1.1).

I'd like to have a simple python program on the phone that can send files to the computer and get files from the computer. The phone end should only run when invoked, the computer end can be a server, although preferably something that does not use a lot of resources. The phone end should be able to figure out what's in the relevant directory on the computer.

At the moment I'm getting files from computer to phone by running windows web server on the computer (ugh) and a script with socket.set_ default _ access_point (so the program can pick my router's ssid or other transport) and urlretrieve (to get the files) on the phone. I'm sending files the other way by email using smtplib.

Suggestions would be appreciated, whether a general idea, existing programs or anything in between.


Solution

  • I ended up using python's ftplib on the phone and FileZilla, an ftp sever, on the computer. Advantages are high degree of simplicity, although there may be security issues.

    In case anyone cares, here's the guts of the client side code to send and receive files. Actual implementation has a bit more infrastructure.

    from ftplib import FTP
    import os
    
    ftp = FTP()
    ftp.connect(server, port)
    ftp.login(user, pwd)
    
    files = ftp.nlst() # get a list of files on the server
    # decide which file we want
    
    fn = 'test.py' # filename on server and for local storage
    d = 'c:/temp/' # local directory to store file
    path = os.path.join(d,fn)
    r = ftp.retrbinary('RETR %s' % fn, open(path, 'wb').write)
    print(r) # should be: 226 Transfer OK
    
    f = open(path, 'rb') # send file at path
    r = ftp.storbinary('STOR %s' % fn, f) # call it fn on server
    print(r) # should be: 226 Transfer OK
    f.close()
    
    ftp.quit()