【发布时间】: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