使用Python的檔案共享應用


藍牙和WhatsApp通常用於在裝置之間傳送檔案。雖然這兩種方法都非常方便,但是隻需一個簡單的二維碼就能訪問其他裝置的文件,這令人感到非常有趣。尤其是在幾分鐘內使用Python就能實現這一點。

藉助Python結合計算機網路的概念,我們可以建立一個簡單的Python應用程式來在不同裝置之間共享文件。此應用程式將同時提供IP地址和二維碼。根據需要,您可以掃描程式碼或在您選擇的裝置中輸入IP地址,主機將提供對其所有文件的訪問許可權。此應用程式使用了各種Python模組,例如HTTPServer、socketserver、webbrowser、pyqrcode、OS模組和PyPNG。這些模組有助於處理各種埠、套接字、URL和網頁協議。

示例

在此示例中,我們將使用TCP埠8010來設定兩個裝置之間的通訊鏈路。這是透過首先設定我們希望共享其檔案的目錄,然後查詢系統的IP地址來完成的。此外,我們將此IP地址轉換為二維碼,然後將其託管在Web瀏覽器中。然後,我們將使用我們選擇的裝置(在本例中為手機)掃描程式碼,從而從手機訪問系統的所有檔案。

這些是我們將在程式碼中使用的模組和庫:

  • Http.server − 對於任何透過Web瀏覽器進行的傳輸,都需要一個HTTP套接字。Http.server模組有助於建立此套接字並偵聽傳遞到此套接字的內容。

  • Socket和socketserver − 這些模組用於建立和管理網路連線。socket模組提供對作業系統上存在的網路套接字的訪問,而socketserver模組有助於建立網路伺服器。

  • Webbrowser − 此模組有助於設計所有文件的簡單網頁檢視。

  • Pyqrcode − 此模組有助於建立二維碼。

  • Png − 由於我們使用的是將在瀏覽器中顯示為影像的二維碼,因此png模組將有助於使用Python讀取和寫入影像檔案。

  • OS − OS模組有助於與作業系統互動,並允許處理檔案、目錄、環境變數、路徑和程序等。

此外,我們使用TCP埠8010是因為它使用預定義的協議,有助於在各種型別的應用程式之間進行通訊。

演算法

  • 步驟1 − 匯入所有必需的庫和模組。

  • 步驟2 − 分配埠號和系統名稱。

  • 步驟3 − 根據需要更改路徑。

  • 步驟4 − 建立一個處理程式以從指定的目錄提供檔案。

  • 步驟5 − 建立HTTP請求。

  • 步驟6 − 查詢PC的IP地址,並使用pyqrcode模組將其轉換為二維碼。

  • 步驟7 − 在瀏覽器中顯示二維碼。

  • 步驟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

掃描二維碼後,您將看到系統中的所有檔案現在都可以在其他裝置上訪問。這是一個螢幕截圖。

您可以看到,我們現在可以訪問我們選擇的目錄中的所有檔案。

結論

使用Python建立檔案共享應用程式最簡單的方法是使用二維碼。這種方法的唯一缺點是,在設定目錄路徑或開啟通訊埠時可能會遇到錯誤,因為它只能開啟一次。但是,考慮到所需的時間,即使與WhatsApp Web相比,這種方法也更簡單。其他此類應用程式包括在客戶端和伺服器機器上使用不同的程式碼來建立連線,從而避免使用手機。

更新於:2023年8月23日

瀏覽量:1000+

啟動您的職業生涯

完成課程獲得認證

開始
廣告