Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from tkinter import filedialog, Tk, Label, Button
- import paramiko
- import urllib.parse
- import os
- from pathlib import Path
- class MyGUI:
- def __init__(self, master):
- self.master = master
- master.title("Server Uploader")
- master.resizable(height=False, width=False)
- master.configure(bg="white")
- self.label = Label(master, text="There was a problem connecting to the server. Please try again later or contact me.", fg="black", bg="white")
- self.label.config(font=("Courier", 12))
- self.label.pack()
- class SuccessfulUpload:
- def __init__(self, master):
- self.master = master
- master.title("Successful Upload")
- master.resizable(height=False,width=False)
- self.label = Label(master, text="Upload successful. You can now close this window.")
- self.label.config(font=("Courier, 12"))
- self.label.pack()
- #Connect to server using SFTP
- ssh_client = paramiko.SSHClient()
- ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- ssh_client.connect(hostname='hostname',port=portno,username='username',password='password')
- except paramiko.ssh_exception.AuthenticationException as e:
- root = Tk()
- my_gui = MyGUI(root)
- root.mainloop()
- raise SystemExit
- #Ask for user to choose files to upload
- root = Tk()
- root.withdraw()
- files = filedialog.askopenfilenames(title="Choose Files to Upload to the Server", filetypes=(("All Files","*.*"),("PDF Files","*.pdf"), ("Word Files","*.doc*")))
- print (root.tk.splitlist(files))
- s = ssh_client.open_sftp()
- if files == "":
- raise SystemExit
- #Define local and remote path for file
- for file in files:
- localpath = file
- parts = Path(file).parts
- endpath = parts[-1]
- print(endpath)
- remotepath="/Path/" + endpath
- print(remotepath)
- s.put(localpath,remotepath)
- root = Tk()
- fin = SuccessfulUpload(root)
- root.mainloop()
- s.close()
- root.destroy()
- complete = input("Upload Successful. Press any key to exit.")
Add Comment
Please, Sign In to add comment