from socket import *
from tkinter import *



def StartScanner(event):
    def CloseTopWin():
        ResultW.destroy()
    ResultW=Toplevel(Window,width=15,height=20,bg='black')
    ResultW.title('შედეგი')
    ResultW.resizable(0,0)
    ResT=Text(ResultW,width=15,height=20,bg='black',fg='green')
    ResCloseB=Button(ResultW,text='დახურვა',bg='black',fg='green',command=CloseTopWin)
    ResT.grid(row=0,column=0,sticky='nswe')
    ResCloseB.grid(row=1,column=0,sticky='nswe')
    if ScanE.get()=='' or StartPortE.get()=='' or EndPortE.get()=='':
        ResT.insert(END,'შეცდომა!!!\nკორექტულად მიუთითეთ აიპი და პორტის მისამართები')
    else:
        for Port in range(int(StartPortE.get()),int(EndPortE.get())+1):
            s=socket(AF_INET,SOCK_STREAM)
            s.settimeout(0.4)
            try:
                s.connect((ScanE.get(),Port))
                txt="პორტი %d: ღიაა!\n" % Port
                ResT.insert(END,txt)
                s.close()
            except:
                print("%d Port Is Closed" % Port)
        print("Finished!!!")
        ResT.configure(state=DISABLED)


Window=Tk()
Window.title('Python Port Scanner V1.0 By Sword1sh')
Window.configure(bg='grey')
Window.resizable(0,0)


ScanL=Label(Window,text='მისამართი: ',bg='grey')
ScanE=Entry(Window,width=30)
PortsL=Label(Window,text='პორტების დიაპაზონი',bg='grey')
StartPortE=Entry(Window,width=10)
EndPortE=Entry(Window,width=10)
TirL=Label(Window,text='-',bg='grey')
EmptL=LabelFrame(Window,width=25,bg='grey')
ScanB=Button(Window,text='სკანირება')

ScanB.bind("<Button-1>",StartScanner)
EndPortE.bind("<Return>",StartScanner)

ScanL.grid(row=0,column=0,sticky='nswe')
ScanE.grid(row=0,column=1,columnspan=3,sticky='nswe')
PortsL.grid(row=0,column=4,sticky='nswe')
StartPortE.grid(row=0,column=5,sticky='nswe')
TirL.grid(row=0,column=6,sticky='nswe')
EndPortE.grid(row=0,column=7,sticky='nswe')
EmptL.grid(row=0,column=8,sticky='nswe')
ScanB.grid(row=0,column=9,sticky='nswe')

Window.mainloop()