【问题标题】:UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 328:UnicodeDecodeError:“charmap”编解码器无法解码位置 328 中的字节 0x81:
【发布时间】:2021-10-12 20:09:13
【问题描述】:

好的,所以我正在为我的房子制作云存储,但问题是它只接收 .txt 或只有文本的文件。我希望它接收 zip 文件图像和所有类型的文件,但是当我尝试发送 zip 文件或任何其他类型的文件时...我收到此错误。

Traceback (most recent call last):
  File "D:\Workspace\Code\Cloud Storage\client.py", line 45, in <module>
    file_data = in_file.read()
  File "C:\Users\Shashankh\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 328: character maps to <undefined>

我确实收到了文件,但文件已损坏或为空。

这是我的代码。

client.py

import socket
import pyautogui
from tkinter import *
from tkinter import filedialog
import os



s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.108',4321))


root = Tk()
root.withdraw()

while True:
   confirm_send = pyautogui.confirm("Do you want to start file transfer?")
   if confirm_send == 'OK':
       warning = pyautogui.confirm("WARNING!: IF YOU HAVE ALREADY SENT THE FILE WITH THE SAME NAME... THE DATA MAY OVERWRITE AND MAY GET CORRUPTED. CHANGE YOUR FILENAME FOR SAFETY!")

       if warning == 'OK':

           prompt = pyautogui.prompt(text='Cloud Name', title='' , default='Enter your cloud storage folder name')
           if prompt == 'Shashankh':
               s.send(bytes('Shashankh','utf-8'))
           if prompt == 'Hari':
               s.send(bytes('Hari','utf-8'))
           if prompt == 'Eshitha':
               s.send(bytes('Eshitha','utf-8'))
           if prompt == 'Prasanna':
               s.send(bytes('Prasanna','utf-8'))
           
           if prompt == None:
               exit()

           file = filedialog.askopenfilename()
           if file == 'Cancel':
               quit()

           file_name = os.path.basename(file)
           s.send(bytes(file_name,'utf-8'))
           open_file = (file, 'rb')        

           with open(file) as in_file:
               file_data = in_file.read()
               s.send(bytes(file_data,'utf-8'))                     
               
           pyautogui.alert("File Transferred succefully!!")
       
       if warning == 'Cancel':
           exit()

   if confirm_send == 'Cancel':
       quit()
   break
       

server.py

import socket
import pyautogui
from pyautogui import *
from tkinter import *
from tkinter import filedialog
import os
from infi.systray import SysTrayIcon

systray = SysTrayIcon("cloud.ico", "Cloud Storage")
systray.start()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0',4321))
s.listen(5)

root = Tk()
root.withdraw()

while True:
    clientsocket, address = s.accept()
    print(f"Connecting from {address} has been established")

    #getting folder name
    msg0 = clientsocket.recv(1024)
    folder_name = (msg0.decode('utf-8'))

    #getting file name
    msg = clientsocket.recv(1024)
    file_name = (msg.decode("utf-8"))

    #getting file data
    msg2 = clientsocket.recv(1024)
    file_data = msg2.decode('utf-8')

    #checking folder name
    if folder_name == 'Shashankh':
        a = open(f"D:\\Cloud Storage\\Shashankh\\{file_name}", "wb")
        a.write(bytes(file_data,'utf-8'))
        a.close()

    elif folder_name == 'Eshitha':
        a = open(f"D:\\Cloud Storage\\Eshitha\\{file_name}", "wb")
        a.write(file_data)
        a.close()    

    elif folder_name == 'Hari':
        a = open(f"D:\\Cloud Storage\\Hari\\{file_name}", "wb")
        a.write(file_data)
        a.close()

    elif folder_name == 'Prasanna':
        a = open(f"D:\\Cloud Storage\\Prasanna\\{file_name}", "wb")
        a.write(file_data)
        a.close()


    pyautogui.alert("File recieved successfully! | Either you have recieved a file or Transfer has been aborted by user |")

【问题讨论】:

    标签: python sockets cloud file-transfer


    【解决方案1】:

    首先是精彩的节目!

    您似乎在 client.py 文件的第 45 行中遗漏了一个关键字。如下
    s.send(bytes(file_name,'utf-8')) 您必须提到一个称为编码的参数。这将访问编码格式(即utf-8)所以下面是更正的代码..
    s.send(bytes(file_name,encoding='utf-8'))

    希望它有效。我很想看到它起作用。

    【讨论】:

    • 嗨!感谢您的赞赏。我遇到的问题与文件名无关,即somefile.zip 问题正在发送文件。当我发送一个 zip 文件时,我得到了那个错误,请再次检查我的代码。我已经编辑了它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2017-02-06
    • 2021-05-06
    • 2021-04-11
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多