python 使用python在电脑和手机之间通过wifi进行简单的文件传输

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1451849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:17:16  来源:igfitidea点击:

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

pythonmobile-phonesfile-transfer

提问by foosion

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

我希望能够在我的手机和电脑之间传输文件。手机是可以运行python 2.5.4的智能手机,电脑运行的是windows xp(python 2.5.4和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.

我想在手机上有一个简单的python程序,可以向计算机发送文件并从计算机获取文件。电话端应该只在被调用时运行,计算机端可以是一个服务器,尽管最好是不使用大量资源的东西。手机端应该能查出电脑上相关目录下的内容。

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.

目前,我通过在计算机上运行 Windows Web 服务器(呃)和带有 socket.set_default_access_point 的脚本(这样程序可以选择我的路由器的 ssid 或其他传输)和 urlretrieve(到获取文件)在手机上。我使用 smtplib 通过电子邮件以另一种方式发送文件。

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

建议将不胜感激,无论是一般想法、现有程序还是介于两者之间的任何内容。

采纳答案by foosion

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.

我最终在手机上使用了 python 的 ftplib,在计算机上使用了 FileZilla,一个 ftp 服务器。优点是高度简单,尽管可能存在安全问题。

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()

回答by rh0dium

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

我会使用paramiko。它安全快速且非常简单。这个怎么样?

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:

我们打开一个 SSH 传输:

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:

另一种方法是使用 SSH 密钥:

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 客户端:

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 连接和传输:

sftp.close()
transport.close()

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

怎么样??对于这个例子,我必须赞扬这一点。

回答by Esteban Küber

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

几个例子,但你必须记住,IIRC,PyBluez 只能在 Linux 上运行。

I've previously done OBEX-related things, mostly fetching things from mobile phones, using the obexftp program 2which 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.

我以前做过与 OBEX 相关的事情,主要是从手机上取东西,使用的是 OpenOBEX 项目3 的一部分obexftp 程序2。当然,您可以从 Python 调用 obexftp 程序,并使用 os、popen2 和 subprocess 模块中的函数解释响应和退出代码。我相信 obexftp 也支持“推送”模式,但如果不支持,您可能会找到与 OpenOBEX 相关的其他内容。

由于在 GNU/Linux 发行版和 Python 中使用套接字支持蓝牙通信(前提是检测和配置了蓝牙支持),您可以使用普通网络编程与手机通信,但这可能需要您自己实现 OBEX 协议 -由于多种原因,这不是一项简单的任务,包括我在下面提到的一个。因此,至少在最初使用 obexftp 可能更容易。

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

你还有lightblue,这是一个跨操作系统的蓝牙库。

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

还有一个完整的脚本,PUTools: Python Utility Tools for PyS60 Python(例子有 Windows 屏幕截图),它有:

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

Python 解释器接受输入并在 PC 上显示输出,通过蓝牙连接到手机,并在手机上执行。您还可以获得手机的简单外壳功能(cd、ls、rm 等)。该工具还允许您将文件从 PC 同步到手机(在应用程序开发中非常有用)以及从手机到 PC(您的图像、您正在处理的程序中的日志文件等)。