views:

3468

answers:

4

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.

A: 

There are a couple of examples out there, but you have to keep in mind that, IIRC, PyBluez will work only on Linux.

I've previously done OBEX-related things, mostly fetching things from mobile phones, using the obexftp program [2] which is part of the OpenOBEX project [3]. Naturally, you can call the obexftp program from Python and interpret the responses and exit codes using functions in the os, popen2 and subprocess modules. I believe that obexftp also supports "push" mode, but you could probably find something else related to OpenOBEX if it does not.

Since Bluetooth communications are supported using sockets in GNU/ Linux distributions and in Python (provided that the Bluetooth support is detected and configured), you could communicate with phones using plain network programming, but this would probably require you to implement the OBEX protocols yourself - not a straightforward task for a number of reasons, including one I mention below. Thus, it's probably easier to go with obexftp at least initially.

You also have lightblue, that is a cross-os bluetooth library.

There is also a complete script, PUTools: Python Utility Tools for PyS60 Python (examples has Windows screenshots), that has a:

Python interpreter that takes input and shows output on PC, connects over Bluetooth to phone, and executes on the phone. You also get simple shell functionality for the phone (cd, ls, rm, etc.). The tool also allows you to synchronize files both from PC to phone (very useful in application development) and from phone to PC (your images, logfiles from the program you are working on, etc.).

voyager
I have an S60 phone with PyS60 and had looked at bluetooth alternatives, both homebrew and PUTools. Alas, bluetooth does not work that well - it takes time to establish a connection and is not very reliable. As you say, reinventing OBEX is not exactly straightforward. That's why I prefer wifi.
foosion
+2  A: 

Hi There,

I would use paramiko. It's secure fast and really simple. How bout this?

So we start by importing the module, and specifying the log file:

import paramiko
paramiko.util.log_to_file('/tmp/paramiko.log')

We open an SSH transport:

host = "example.com"
port = 22
transport = paramiko.Transport((host, port))

Next we want to authenticate. We can do this with a password:

password = "example101"
username = "warrior"
transport.connect(username = username, password = password)

Another way is to use an SSH key:

import os
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
username = 'warrior'
transport.connect(username = username, pkey = mykey)

Now we can start the SFTP client:

sftp = paramiko.SFTPClient.from_transport(transport)

Now lets pull a file across from the remote to the local system:

filepath = '/home/zeth/lenna.jpg'
localpath = '/home/zeth/lenna.jpg'
sftp.get(filepath, localpath)

Now lets go the other way:

filepath = '/home/zeth/lenna.jpg'
localpath = '/home/zeth/lenna.jpg'
sftp.put(filepath, localpath)

Lastly, we need to close the SFTP connection and the transport:

sftp.close()
transport.close()

How's that?? I have to give credit to this for the example.

rh0dium
This got me thinking about using plain old ftp, with a simple server such as FileZilla on the computer and python's ftplib on the phone. I'll have to think about security issues and whether paramiko offers anything else of use. thanks
foosion
A: 

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()
foosion
A: 

http://operation420.net/forum/viewtopic.php?f=56&t=1189

Can this be modified to go from Pocket PC to Pocket PC?

MPG