I know this has been addressed many times and either the solutions don’t apply to me or I cant wrap my head around the solution. I get the error whenever I send my data to the client, It is partially time sensitive. Thanks for the help.
import tkinter as tk
import tkinter.filedialog
from tkinter import *
from tkinter import ttk
import os
import socket
import pickle
def main(TYPE, IP, PORT):
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
clientsocket.connect((IP, PORT))
print("Client Connected to:", IP + ":" + str(PORT))
except socket.error as e:
print(f"Connection error: {e}")
return
def send_data():
files = tkinter.filedialog.askopenfiles(
filetypes=[("Portable Network Graphics", "*.png"), ("JavaScript Object Notation", "*.json"),
("Vorbis Compressed Audio File", "*.ogg")])
files_data = []
for file in files:
file_path = file.name
try:
with open(file_path, "rb") as f:
file_data = f.read()
files_data.append((file_path, file_data))
except IOError as e:
print(f"Error reading file {file_path}: {e}")
try:
if clientsocket:
clientsocket.send(pickle.dumps(files_data))
print("Data sent successfully.")
else:
print("Socket is not connected.")
except socket.error as e:
print(f"Send error: {e}")
def recv_data():
try:
data = clientsocket.recv(2048)
if not data:
print("No data received or socket might be closed.")
return
files = pickle.loads(data)
for data_bundle in files:
file_path = data_bundle[0]
file_data = data_bundle[1]
file_path_minus_name = os.path.dirname(file_path)
try:
os.makedirs(file_path_minus_name, exist_ok=True)
except OSError as e:
print(f"Error creating directory {file_path_minus_name}: {e}")
try:
with open(file_path, "wb") as new_file:
new_file.write(file_data)
print(f"File received and written: {file_path}")
except IOError as e:
print(f"Error writing file {file_path}: {e}")
except socket.error as e:
print(f"Receive error: {e}")
except pickle.PickleError as e:
print(f"Pickle error: {e}")
def choose_resource_folder():
global file_path
file_path = tkinter.filedialog.askdirectory()
print(f"Selected folder: {file_path}")
folder_label = ttk.Label(main_frame, text=file_path).grid(column=0, row=1)
main_window = Tk()
main_window.resizable(0, 0)
main_window.geometry("590x590")
main_frame = ttk.Frame(main_window, padding=10)
main_frame.grid()
choose_folder = ttk.Button(main_frame, text="Choose Folder", command=choose_resource_folder).grid(column=0, row=0)
send_button = ttk.Button(main_frame, text="Send Files", command=send_data).grid(column=1, row=0)
recv_button = ttk.Button(main_frame, text="Recv Files", command=recv_data).grid(column=2, row=0)
main_window.mainloop()
if __name__ == "__main__":
main("server", "127.0.0.1", 8080)
Tried a million more tries and exceptions then i needed to. I don’t know why i thought it would help.
New contributor
pru1ne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2