Advertisement
Guest User

Untitled

a guest
Feb 16th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. import ftplib
  2. import os
  3. import tkinter as tk
  4. from tkinter import filedialog, messagebox
  5.  
  6. class SimpleFTPClient:
  7.     def __init__(self, root):
  8.         self.root = root
  9.         self.root.title("Simple FTP Client")
  10.        
  11.         self.ftp = None
  12.         self.current_dir = "/"
  13.        
  14.         # Connect Frame
  15.         self.connect_frame = tk.Frame(root)
  16.         self.connect_frame.pack(padx=10, pady=10)
  17.  
  18.         self.host_label = tk.Label(self.connect_frame, text="FTP Host:")
  19.         self.host_label.grid(row=0, column=0, padx=5, pady=5)
  20.  
  21.         self.host_entry = tk.Entry(self.connect_frame)
  22.         self.host_entry.grid(row=0, column=1, padx=5, pady=5)
  23.  
  24.         self.user_label = tk.Label(self.connect_frame, text="Username:")
  25.         self.user_label.grid(row=1, column=0, padx=5, pady=5)
  26.  
  27.         self.user_entry = tk.Entry(self.connect_frame)
  28.         self.user_entry.grid(row=1, column=1, padx=5, pady=5)
  29.  
  30.         self.pass_label = tk.Label(self.connect_frame, text="Password:")
  31.         self.pass_label.grid(row=2, column=0, padx=5, pady=5)
  32.  
  33.         self.pass_entry = tk.Entry(self.connect_frame, show="*")
  34.         self.pass_entry.grid(row=2, column=1, padx=5, pady=5)
  35.  
  36.         self.connect_button = tk.Button(self.connect_frame, text="Connect", command=self.connect_to_ftp)
  37.         self.connect_button.grid(row=3, columnspan=2, pady=10)
  38.  
  39.         # File Upload Frame
  40.         self.upload_frame = tk.Frame(root)
  41.         self.upload_frame.pack(padx=10, pady=10)
  42.  
  43.         self.upload_button = tk.Button(self.upload_frame, text="Upload File", command=self.upload_file)
  44.         self.upload_button.grid(row=0, column=0, padx=5, pady=5)
  45.  
  46.         # File List Frame
  47.         self.file_listbox = tk.Listbox(root, height=10, width=50)
  48.         self.file_listbox.pack(padx=10, pady=10)
  49.  
  50.     def connect_to_ftp(self):
  51.         try:
  52.             host = self.host_entry.get()
  53.             username = self.user_entry.get()
  54.             password = self.pass_entry.get()
  55.            
  56.             self.ftp = ftplib.FTP(host)
  57.             self.ftp.login(username, password)
  58.             self.list_files()
  59.             messagebox.showinfo("Connection", "Successfully connected to the FTP server.")
  60.         except Exception as e:
  61.             messagebox.showerror("Error", f"Failed to connect: {str(e)}")
  62.  
  63.     def list_files(self):
  64.         self.file_listbox.delete(0, tk.END)
  65.        
  66.         if not self.ftp:
  67.             return
  68.        
  69.         self.ftp.cwd(self.current_dir)
  70.         files = self.ftp.nlst()
  71.        
  72.         for file in files:
  73.             self.file_listbox.insert(tk.END, file)
  74.  
  75.     def upload_file(self):
  76.         if not self.ftp:
  77.             messagebox.showerror("Error", "Not connected to FTP server.")
  78.             return
  79.  
  80.         file_path = filedialog.askopenfilename(title="Select a file to upload")
  81.         if not file_path:
  82.             return
  83.  
  84.         file_name = os.path.basename(file_path)
  85.        
  86.         try:
  87.             with open(file_path, "rb") as file:
  88.                 self.ftp.storbinary(f"STOR {file_name}", file)
  89.             self.list_files()
  90.             messagebox.showinfo("Success", f"File '{file_name}' uploaded successfully.")
  91.         except Exception as e:
  92.             messagebox.showerror("Error", f"Failed to upload the file: {str(e)}")
  93.  
  94.  
  95. if __name__ == "__main__":
  96.     root = tk.Tk()
  97.     ftp_client = SimpleFTPClient(root)
  98.     root.mainloop()
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement