Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 292
foosion Avatar asked Sep 20 '09 19:09

foosion


People also ask

Can we transfer files using Wi-Fi from laptop to mobile?

Part 2: Transfer Files via Wifi-Direct After that, connect your PC to the same network. Launch the Feem application on your PC and Android devices. You will see them connected already once you open the application. Now, select your Android device and click on Send Files option.

Can I share files over Wi-Fi between PC and Android?

The best way to transfer files between your PC and Android is to send them over a shared wireless network. You can use many apps for this, such as Wi-Fi File Transfer on Android and Air Transfer on iOS. Some file browsers also incorporate Wi-Fi file transfers.


1 Answers

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.

like image 160
rh0dium Avatar answered Oct 04 '22 14:10

rh0dium