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

发布于 2024-08-05 10:43:17 字数 413 浏览 5 评论 0原文

我希望能够在手机和电脑之间传输文件。手机是可以运行 python 2.5.4 的智能手机,计算机运行的是 windows xp(带有 python 2.5.4 和 3.1.1)。

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

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

无论是总体想法、现有计划还是介于两者之间的任何建议,我们将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2024-08-12 10:43:17

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

因此,我们首先导入模块并指定日志文件:

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

我们打开 SSH 传输:

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

接下来我们要进行身份验证。我们可以使用密码来完成此操作:

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

另一种方法是使用 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)

现在我们可以启动 SFTP 客户端:

sftp = paramiko.SFTPClient.from_transport(transport)

现在让我们将文件从远程拉到本地系统:

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

现在让我们换一种方式:

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

最后,我们需要关闭 SFTP 连接和传输:

sftp.close()
transport.close()

怎么样?对于这个例子,我必须给予信用

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.

窝囊感情。 2024-08-12 10:43:17

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

如果有人关心的话,这里是发送和接收文件的客户端代码的核心。实际的实施需要更多的基础设施。

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

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()
不乱于心 2024-08-12 10:43:17

一些示例,但是您必须记住,IIRC、PyBluez 只能在 Linux 上运行。

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

由于使用 GNU/ 中的套接字支持蓝牙通信
Linux 发行版和 Python 版本(前提是蓝牙支持
被检测到并配置),您可以使用以下方式与电话通信
简单的网络编程,但这可能需要您
自己实现 OBEX 协议 - 对于
有很多原因,包括我在下面提到的一个。因此,它是
至少最初使用 obexftp 可能更容易。

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文