Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ftplib
- import os
- import tkinter as tk
- from tkinter import filedialog, messagebox
- class SimpleFTPClient:
- def __init__(self, root):
- self.root = root
- self.root.title("Simple FTP Client")
- self.ftp = None
- self.current_dir = "/"
- # Connect Frame
- self.connect_frame = tk.Frame(root)
- self.connect_frame.pack(padx=10, pady=10)
- self.host_label = tk.Label(self.connect_frame, text="FTP Host:")
- self.host_label.grid(row=0, column=0, padx=5, pady=5)
- self.host_entry = tk.Entry(self.connect_frame)
- self.host_entry.grid(row=0, column=1, padx=5, pady=5)
- self.user_label = tk.Label(self.connect_frame, text="Username:")
- self.user_label.grid(row=1, column=0, padx=5, pady=5)
- self.user_entry = tk.Entry(self.connect_frame)
- self.user_entry.grid(row=1, column=1, padx=5, pady=5)
- self.pass_label = tk.Label(self.connect_frame, text="Password:")
- self.pass_label.grid(row=2, column=0, padx=5, pady=5)
- self.pass_entry = tk.Entry(self.connect_frame, show="*")
- self.pass_entry.grid(row=2, column=1, padx=5, pady=5)
- self.connect_button = tk.Button(self.connect_frame, text="Connect", command=self.connect_to_ftp)
- self.connect_button.grid(row=3, columnspan=2, pady=10)
- # File Upload Frame
- self.upload_frame = tk.Frame(root)
- self.upload_frame.pack(padx=10, pady=10)
- self.upload_button = tk.Button(self.upload_frame, text="Upload File", command=self.upload_file)
- self.upload_button.grid(row=0, column=0, padx=5, pady=5)
- # File List Frame
- self.file_listbox = tk.Listbox(root, height=10, width=50)
- self.file_listbox.pack(padx=10, pady=10)
- def connect_to_ftp(self):
- try:
- host = self.host_entry.get()
- username = self.user_entry.get()
- password = self.pass_entry.get()
- self.ftp = ftplib.FTP(host)
- self.ftp.login(username, password)
- self.list_files()
- messagebox.showinfo("Connection", "Successfully connected to the FTP server.")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to connect: {str(e)}")
- def list_files(self):
- self.file_listbox.delete(0, tk.END)
- if not self.ftp:
- return
- self.ftp.cwd(self.current_dir)
- files = self.ftp.nlst()
- for file in files:
- self.file_listbox.insert(tk.END, file)
- def upload_file(self):
- if not self.ftp:
- messagebox.showerror("Error", "Not connected to FTP server.")
- return
- file_path = filedialog.askopenfilename(title="Select a file to upload")
- if not file_path:
- return
- file_name = os.path.basename(file_path)
- try:
- with open(file_path, "rb") as file:
- self.ftp.storbinary(f"STOR {file_name}", file)
- self.list_files()
- messagebox.showinfo("Success", f"File '{file_name}' uploaded successfully.")
- except Exception as e:
- messagebox.showerror("Error", f"Failed to upload the file: {str(e)}")
- if __name__ == "__main__":
- root = tk.Tk()
- ftp_client = SimpleFTPClient(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement