Homework 5 Codes:

#141180018-KURSAT CAKAL
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk
import os
import ftplib
import sys

         
def ftpClientFile_Form():
     directory=filedialog.askdirectory() #istemci olarak dosya uzantısı secmek icin      
     print (directory)
     for roots,dirs,files in os.walk(directory): #secilen directory icinde dolasmak icin  
        for file in files:
            ftpDosyaListesi.insert(END,file) #istemci olarak secilen dosyaları listeye eklemek icin   
        for direc in dirs:
            ftpDosyaListesi.insert(END,direc) #istemci olarak secilen uzantidaki directoryleri listeye eklemek icin
     ftpDosyaListesi.insert(END,'*splitter*')
def runFTP():
     command=ftpUnsiplettedCommandText.get()    
     commands=command.split()    
     for i in commands:
         print(i)
     if True:        
         ftp=ftplib.FTP(hostID.get())
         ftp.login(username.get(),password.get())
         #LS,CD,EXIT,MKDIR,GET,DELETE,RMD,PUT,RENAME
         if combobox_for_Command_selection.get()=='LS': #dosya listeleme FTP komutu icin.
                 ftp.dir()
                 for direc in ftp.nlst():
                     ftpDosyaListesiServer.insert(END,direc)
                 ftpDosyaListesiServer.insert(END,'*splitter*')

         if combobox_for_Command_selection.get()=='CD': #directory giris FTP komutu icin.
            try:
                ftp.cwd(commands[0])
                ftp.dir()
                for direc in ftp.nlst():
                    ftpDosyaListesiServer.insert(END,direc)
                ftpDosyaListesiServer.insert(END,ftp.pwd())
                ftpDosyaListesiServer.insert(END,'Basarili')
            except ftplib.error_perm as e:
                error_code=str(e).split(None,1)
                if error_code[0]=='550':
                    ftpDosyaListesiServer.insert(END,error_code[1]+'directory yanlis')
                    ftpDosyaListesiServer.insert(END,'*splitter*')
         if combobox_for_Command_selection.get()=='EXIT': #ftp baglantisindan cikmak icin.
                ftp.quit()
                print('FTP Baglantisi Kapandi')
                ftpDosyaListesiServer.insert(END,'FTP Baglantisi Kapandi')
         if combobox_for_Command_selection.get()=='MKDIR': 
                 ftp.mkd('/'+commands[0])
                 ftpDosyaListesiServer.insert(END,'Basarili')
                 ftp.dir()
                 for direc in ftp.nlst():
                     ftpDosyaListesiServer.insert(END,direc)
                 ftpDosyaListesiServer.insert(END,'*splitter*')
         if combobox_for_Command_selection.get()=='GET': #dosya alma FTP komutu icin.
               try:
                 ftp.retrbinary('RETR '+commands[1], open(commands[0]+commands[1],'wb').write)
                 ftpDosyaListesiServer.insert(END,'Basarili')
               except ftplib.error_perm as e: 
                     error_code=str(e).split(None,1)
                     if error_code[0]=='550':
                        ftpDosyaListesiServer.insert(END,error_code[1]+' dosya bu directoryde yok')
                        ftpDosyaListesiServer.insert(END,'*splitter*')
         if combobox_for_Command_selection.get()=='DELETE': #dosya silme FTP komutu icin.
                 ftp.delete('/'+commands[0])
                 ftpDosyaListesiServer.insert(END,'Basarili')
                 ftp.dir()
                 for direc in ftp.nlst():
                     ftpDosyaListesiServer.insert(END,direc)
                 ftpDosyaListesiServer.insert(END,'*splitter*')
         if combobox_for_Command_selection.get()=='RMD': #dosya silme FTP komutu icin.
                 ftp.rmd('/'+commands[0])
                 ftpDosyaListesiServer.insert(END,'Basarili')
                 ftp.dir()
                 for direc in ftp.nlst():
                     ftpDosyaListesiServer.insert(END,direc)
                 ftpDosyaListesiServer.insert(END,'*splitter*')    
         if combobox_for_Command_selection.get()=='PUT': #dosya upload etme FTP komutu icin.
                 try:
                    ftp.storlines('STOR '+commands[1],open(commands[0],'rb'))
                    ftpDosyaListesiServer.insert(END,'Basarili')
                 except ftplib.error_perm as e:
                     error_code=str(e).split(None,1)
                     if error_code[0]=='550':
                        ftpDosyaListesiServer.insert(END,error_code[1]+'dosya bu directoryde yok')
                        ftpDosyaListesiServer.insert(END,'*splitter*')  
         if combobox_for_Command_selection.get()=='RENAME': #dosya adı degistirme FTP komutu icin.
                 ftp.rename('/'+commands[0],'/'+commands[1])
                 ftpDosyaListesiServer.insert(END,'Basarili')
                 ftp.dir()
                 for direc in ftp.nlst():
                     ftpDosyaListesiServer.insert(END,direc)
                 ftpDosyaListesiServer.insert(END,'*splitter*')  
         else: 
                ftpDosyaListesiServer.insert(END,'Sadece belirtilen komutlar ile islem yapabilirsiniz: LS,CD,EXIT,MKDIR,GET,DELETE,RMD,PUT,RENAME)')
     else: 
         messagebox.showerror('FTP Hatasi','FTP Giris bilgisi giriniz.')
     
def exitFormMethod():
    clientFTP_GUI.destroy()      
    clientFTP_GUI.quit()  
       
def ftpLoginForm():
     if True:
         ftp=ftplib.FTP(hostID.get())        
         ftp.login(username.get(),password.get()) #ftp servera verilen kimlik ile giris yapmak icin
         print('Directory Name',ftp.pwd())   
         ftp.dir()                           
         ftpDosyaListesiServer.insert(END,ftp.getwelcome())
         ftpDosyaListesiServer.insert(END,'Directory Name'+ftp.pwd())
         for direc in ftp.nlst():           
            ftpDosyaListesiServer.insert(END,direc)
         ftpDosyaListesiServer.insert(END,ftp.getwelcome())
     else:
         print('ID,PW,Host Degerleri bos birakilamaz.')           
def comboMetot():
    messagebox.showinfo("Title", combobox_for_Command_selection.get()) #combobox test metodu
           
clientFTP_GUI=Tk()
clientFTP_GUI.title('FTP Client Form')
clientFTP_GUI.geometry('720x600')
clientFTP_GUI.configure(bg='red')
hostID=StringVar() 
username=StringVar() 
password=StringVar() 
ftpUnsiplettedCommandText=StringVar() 
###FTP Sign In
label_hostIP= Label(clientFTP_GUI, text="FTP Server IP:",width=20,font=("bold",10))
label_hostIP.place(x=200,y=10)
label_username= Label(clientFTP_GUI, text="FTP Server Username:",width=20,font=("bold",10))
label_username.place(x=200,y=30)
label_password= Label(clientFTP_GUI, text="FTP Server Password:",width=20,font=("bold",10))
label_password.place(x=200,y=50)
hostEntry=Entry(clientFTP_GUI,textvariable=hostID,width=15,font=("bold",10))
hostEntry.place(x=370,y=10)
usernameEntry=Entry(clientFTP_GUI,textvariable=username,width=15,font=("bold",10))
usernameEntry.place(x=370,y=30)
passwordEntry=Entry(clientFTP_GUI,textvariable=password,width=15,font=("bold",10))
passwordEntry.place(x=370,y=50)
#style = ttk.Style()
#style.map('TCombobox', selectbackground=[('readonly', 'blue')])
btnFTPLogin=Button(clientFTP_GUI,text='FTP Sign in',command=ftpLoginForm,fg='Black',width=15,font=("bold",14))
btnFTPLogin.place(x=250,y=160)
###FTP Sign In
ftpClientDosya=Button(clientFTP_GUI,text='FTP Client File',fg='Black',command=ftpClientFile_Form,width=15,font=("bold",14))
ftpClientDosya.place(x=250,y=220)
###Listbox GUI
ftpDosyaListesi=Listbox(clientFTP_GUI,width=25,height=30)
ftpDosyaListesi.place(x=40,y=140)
ftpDosyaListesiServer=Listbox(clientFTP_GUI,width=25,height=30)
ftpDosyaListesiServer.place(x=466,y=140)
###Listbox GUI
###Combobox GUI
combobox_for_Command_selection=ttk.Combobox(clientFTP_GUI,values=("LS","CD","EXIT","MKDIR","GET","DELETE","RMD","PUT","RENAME"),width=16,background="blue",foreground="green",font=("bold",14))
combobox_for_Command_selection.set("FTP Komutu Seciniz")
combobox_for_Command_selection.place(x=250,y=280)
###Combobox GUI
###Command GUI
entryFTPcommand=Entry(clientFTP_GUI,textvariable=ftpUnsiplettedCommandText,width=17,font=("bold",14))
entryFTPcommand.place(x=250,y=340)
###Command GUI
###FTP Run GUI
btnFTPKomutRunEt=Button(clientFTP_GUI,text='FTP Run',command=runFTP,fg='Black',width=15,font=("bold",14))
btnFTPKomutRunEt.place(x=250,y=400)
btnFTPSignout=Button(clientFTP_GUI,text='FTP Log Out',command=exitFormMethod,fg='Black',width=15,font=("bold",14))
###FTP Run GUI
###Exit GUI
btnFTPSignout.place(x=250,y=460)
###Exit GUI
###Form MainLoop GUI
clientFTP_GUI.mainloop()        

Homework Dosyaları :