Python 使用文件共享应用程序
蓝牙和WhatsApp经常被用来在设备之间发送文件。虽然这两种方法非常方便,但是只需用一个简单的二维码轻触其他设备的文件就足够有趣了。尤其是,当使用Python只需几分钟就能完成时。
借助Python和计算机网络的概念,我们可以制作一个简单的Python应用程序来在不同设备之间共享文件。该应用程序将提供一个IP地址和一个二维码。按需扫描代码或在所选择的设备上输入IP地址,主机将提供对其所有文件的访问权限。该应用程序使用了各种Python模块,如HTTPServer, socketserver, webbrowser, pyqrcode, OS模块和PyPNG。这些模块帮助处理各种端口、插座、URL和网页协议。
示例
在这个示例中,我们将使用TCP端口8010来建立两个设备之间的通信链接。首先设置我们要共享文件的目录,然后找到系统的IP地址。然后,将此IP地址转换为一个二维码,并将其托管在Web浏览器中。接下来,我们将使用所选择的设备,本例中是手机,扫描该二维码,从我们的手机上访问系统的所有文件。
这些是我们将在代码中使用的模块和库:
- Http.server - 为了在 Web 浏览器上进行任何类型的传输,我们需要一个 HTTP socket。Http.server 模块帮助创建此 socket 并监听发送到此 socket 的内容。
-
Socket 和 socketserver - 这些模块用于创建和管理网络连接。Socket 模块提供了访问操作系统上的网络 socket,而 socketserver 模块则帮助创建网络服务器。
-
Webbrowser - 这个模块帮助设计一个简单的网页视图以显示所有文档。
-
Pyqrcode - 这个模块帮助创建一个二维码。
-
Png - 由于我们使用的二维码将作为图像显示在浏览器上,所以 png 模块可以帮助使用Python读取和写入图片文件。
-
OS - OS 模块帮助与操作系统进行交互,允许处理文件、目录、环境变量、路径和进程等。
此外,我们使用TCP端口8010,因为它使用预定义的协议,有助于在各种类型的应用程序之间进行通信。
步骤
- 步骤1 - 导入所有所需的库和模块。
-
步骤2 - 指定系统的端口号和名称。
-
步骤3 - 根据需要更改路径。
-
步骤4 - 创建一个处理程序,以从指定目录中提供文件。
-
步骤5 - 创建HTTP请求。
-
步骤6 - 找到PC的IP地址并使用pyqrcode模块将其转换为QR码。
-
步骤7 - 在浏览器中显示QR码。
-
步骤8 - 创建HTTP请求以在两台设备之间提供文件夹。
#import modules
#to work with HTTP Web servers
import http.server
#to access BSD socket interface
import socket
#to work with network servers
import socketserver
#to display the documents to user on other device as a web page
import webbrowser
#to generate a qr code
import pyqrcode
from pyqrcode import QRCode
#to convert the code in png format
import png
#to access all directories and os
import os
#assign port
PORT = 8010
#to find the name of user’s system
os.environ['USERPROFILE']
#changing the path
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']))
#handler to serve files from appropriate directory
class DesktopHandler(http.server.SimpleHTTPRequestHandler):
def translate_path(self, path):
#to get the absolute path
root_dir = os.path.abspath(desktop)
#to join the root directory and the given path
path = os.path.normpath(path)
words = path.split('/')
words = filter(None, words)
path = root_dir
for word in words:
if os.path.dirname(word) or word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
#create http request
Handler = DesktopHandler
hostname = socket.gethostname()
#to find the IP address of pc
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
IP = "http://" + s.getsockname()[0] + ":" + str(PORT)
link = IP
#convert the IP address into a Qrcode using pyqrcode
url = pyqrcode.create(link)
url.svg("myqr.svg", scale=8)
webbrowser.open('myqr.svg')
#create http request and serve folders between client and server
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
print("Type this in your Browser", IP)
print("or Use the QRCode")
httpd.serve_forever()
输出
serving at port 8010
Type this in your Browser http://192.168.30:178:8010
or Use the QRCode
扫描QR码后,您将看到系统中的所有文件现在可以在其他设备上访问。以下是屏幕截图。
您可以看到我们现在可以访问所选择的目录中的所有文件。
结论
使用Python创建文件共享应用程序的最简单方法是使用QR码。这种方法的唯一缺点是您可能会在设置目录路径或打开通信端口时遇到错误,因为它只能打开一次。然而,考虑到所需的时间,这种方法甚至比WhatsApp web更简单。其他类似应用程序包括在客户端和服务器机器上使用不同的代码建立连接,从而消除了使用手机的需求。