我正在嘗試將影像檔案從 Raspberry Pi(客戶端)發送到筆記本電腦(服務器)。當我在連接到 LAN 的 Raspberry Pi(Linux 作業系統)上運行 client.py 和在筆記本電腦(Windows 作業系統)上運行 server.py 時,我在筆記本電腦(服務器端)上收到以下錯誤訊息。
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 5: invalid start byte
另一方面,當我在同一臺 Windows 筆記本電腦上運行兩個腳本(server.py 和 client.py)時,我沒有收到任何錯誤并且檔案傳輸成功。
server.py 代碼如下:
import os
import socket
HOST = '192.168.2.80' #Private IP address of laptop
PORT = 3322
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
print("STATUS_MSG: This-Is-Laptop")
print("STATUS_MSG: Awaiting-Connection-From-Client")
server.listen()
try:
communication_socket, addrs_of_client = server.accept()
print(f"STATUS_MSG: Connection-Established-To-Client-IP-{addrs_of_client}")
except:
print("STATUS_MSG: Unable-To-Accept-Connection")
exit(0)
file_name = communication_socket.recv(1024).decode()
print(f"incommming file name = {file_name}")
file_size = communication_socket.recv(1024).decode()
print(f"incommming file size = {file_size}")
file = open("./recvt/" file_name, "wb")
file_bytes = b""
done = False
while not done:
data = communication_socket.recv(1024)
if file_bytes[-5:] == b"<END>":
done = True
else:
file_bytes = data
file.write(file_bytes)
file.close()
print("File Received Successfully")
communication_socket.close()
server.close()
client.py 代碼如下:
import os
import socket
HOST = '192.168.2.80' #IP of the server
PORT = 3322
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((HOST, PORT))
print(f"STATUS_MSG: Connected-Successfully-To-Server-IP-{HOST}")
except:
print("STATUS_MSG: Unable-To-Connect-To-Server")
exit(0) # to end the program
# Getting file details.
file_name = "image1.jpg"
file_size = os.path.getsize(file_name)
client.send(file_name.encode())
client.send(str(file_size).encode())
# Reading file and sending data
file = open(file_name, "rb")
data = file.read()
client.sendall(data)
client.send(b"<END>")
file.close()
client.close()
兩個腳本在 Windows 筆記本電腦上運行時的輸出:
STATUS_MSG: This-Is-Laptop
STATUS_MSG: Awaiting-Connection-From-Client
STATUS_MSG: Connection-Established-To-Client-IP-('192.168.2.80', 58646)
incommming file name = image1.jpg
incommming file size = 81377
File Received Successfully
腳本 client.py 在 raspberry pi 和 server.py 在筆記本電腦上運行時的輸出。
STATUS_MSG: This-Is-Laptop
STATUS_MSG: Awaiting-Connection-From-Client
STATUS_MSG: Connection-Established-To-Client-IP-('192.168.2.197', 59062)
incommming file name = image1.jpg
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
Input In [2], in <cell line: 26>()
24 file_name = communication_socket.recv(1024).decode()
25 print(f"incommming file name = {file_name}")
---> 26 file_size = communication_socket.recv(1024).decode()
27 print(f"incommming file size = {file_size}")
29 file = open("./recvt/" file_name, "wb")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 5: invalid start byte
請指導我如何在此處更正編碼/解碼問題,因為我想擴展此腳本以在筆記本電腦(Windows OS)和Raspberry Pi(raspbian OS)之間來回傳輸多個檔案。謝謝你。
uj5u.com熱心網友回復:
file_name = communication_socket.recv(1024).decode()
這里這個呼叫套接字試圖填充 1024 位元組的緩沖區。僅當 (1) 套接字將狀態從可讀更改為可寫或 (2) 連接關閉時,它才會中斷。
client.send(file_name.encode())
client.send(str(file_size).encode())
# Reading file and sending data
file = open(file_name, "rb")
data = file.read()
client.sendall(data)
但這里的代碼只是發送資料。所以recv的第一次呼叫將在1024位元組后結束,而不是在接收檔案名之后。
錯誤原因:在前 2048 個位元組(每次呼叫 1024 個)中,一些位元組來自檔案,它也是二進制的,無法在 utf-8 中解碼。
修改:第一件事你可以在收到名稱后發送一個確認,然后在收到尺寸后發送另一個。或者您可以在名稱、大小和資料之間使用分隔符。
編輯:如果你只想傳輸影像/檔案,你可以使用內置的 python 的 http 服務器。python3 -m http.server {port}
這將在給定埠啟動 http 服務器并提供當前作業目錄
EDIT2中的檔案:
if file_bytes[-5:] == b"<END>":
出于同樣的原因,這永遠不會發生,因為<END>檔案內容的結尾不會作為單獨的 msg。在客戶端中,您需要在檔案結束后添加確認
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/512337.html
上一篇:無法理解sockets.io房間
