Advertisement
furas

Python - socket + tkinter

Mar 24th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from Tkinter import *
  4. import threading
  5. import socket
  6.  
  7. class Manager(object):
  8.    
  9.     def __init__(self):
  10.        
  11.         self.address = []
  12.         self.connction = []
  13.  
  14.         self.GUI1 = Tk()
  15.         self.GUI1.title('welcome to admin control')
  16.         self.GUI1.geometry('800x800')
  17.  
  18.         self.ListTargets = Listbox(self.GUI1, width=95, fg='green', bg='black', font=2)
  19.         self.ListTargets.pack()
  20.  
  21.         self.butt = Button(self.GUI1, text='reflesh', font=5, command=self.numberConns)
  22.         self.butt.pack(anchor=CENTER)
  23.  
  24.         OpenConnction = threading.Thread(target=self.StartSockets)
  25.         OpenConnction.daemon = True
  26.         OpenConnction.start()
  27.        
  28.         mainloop()
  29.  
  30.     def StartSockets(self):
  31.  
  32.         host = '127.0.0.1'
  33.         port = 4434
  34.  
  35.         while True:
  36.             try:                
  37.                 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38.                
  39.                 # solution for "[Error 89] Address already in use". Use it before bind()
  40.                 self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  41.                
  42.                 self.s.bind((host, port))
  43.                 self.s.listen(20)
  44.                
  45.                 while True:
  46.                     try:
  47.                         conn, addr = self.s.accept()
  48.                         conn.setblocking(1)
  49.                         self.connction.append(conn)
  50.                         self.address.append(addr)
  51.                         print 'hello', addr
  52.                     except Exception, e:
  53.                         print 'problem with accept'
  54.                         print 'exception:', e
  55.  
  56.             except Exception, e:
  57.                 print 'problem with socket, bind or listen'
  58.                 print 'exception:', e
  59.  
  60.                 # close all socket
  61.                
  62.                 for c in self.connction:
  63.                     c.close()
  64.                 self.s.close()
  65.                
  66.                 # clear all lists
  67.                
  68.                 self.connction = []
  69.                 self.address = []
  70.  
  71.     def numberConns(self):
  72.         # remove all elements from listbox
  73.         self.ListTargets.delete(0, 'end')
  74.        
  75.         # add all elements again to listbox
  76.         for addr in self.address:
  77.             self.ListTargets.insert('end', addr)
  78.  
  79. # --- main ---
  80.  
  81. Manager()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement