Advertisement
Guest User

BotNet.py

a guest
Dec 23rd, 2014
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #-------------------------------------#
  5. #BotNet's GUI version, created by _jun#
  6. #-------------------------------------#
  7.  
  8. import wx
  9. from socket import *
  10. import thread
  11. import sys
  12. from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
  13.  
  14.  
  15. #color
  16. global red
  17. red = '\033[31m'
  18. global green
  19. green  = '\033[32m'
  20. global reset
  21. reset = '\033[0m'
  22. global yellow
  23. yellow = '\033[33m'
  24. global cyan
  25. cyan = '\033[36m'
  26. global magenta
  27. magenta = '\033[35m'
  28. global cls
  29. cls = '\33c'
  30. #end color
  31.  
  32.  
  33. class AutoWidthListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin):
  34.     def __init__(self, parent):
  35.         wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT)
  36.         ListCtrlAutoWidthMixin.__init__(self)
  37.  
  38. class Server():
  39.     """ The server class, with his configuration and his functions """
  40.    
  41.     def __init__(self):
  42.         self.host = '192.168.0.20'
  43.         self.port = 55568
  44.         self.buf = 1024
  45.  
  46.         self.addr = (self.host, self.port)
  47.  
  48.         self.serversocket = socket(AF_INET, SOCK_STREAM)
  49.         self.serversocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  50.         self.serversocket.bind(self.addr)
  51.  
  52.     def handler(self, clientsocket, clientaddr):
  53.         self.lastmsg = str(clientsocket.recv(4096))
  54.         print(self.lastmsg)
  55.  
  56.  
  57.     def listen(self, port, serversocket):
  58.         serversocket.listen(5)
  59.  
  60.         token = 1
  61.         while token == 1:
  62.             print("Server is listening on port {}.".format(port))
  63.             self.clientsocket, self.clientaddr = self.serversocket.accept()
  64.             print("Connection from ", self.clientsocket.getpeername())
  65.             thread.start_new_thread(self.handler,(self.clientsocket, self.clientaddr))
  66.             token = 0
  67.  
  68.         self.client_address = str(self.clientaddr)
  69.         cl = Clients(self.clientaddr)
  70.  
  71.            
  72.  
  73. class Clients(Server):
  74.     """ The clients class, which have all clients in it."""
  75.     def __init__(self, address='N/A', lastmsg='N/A', status='N/A'):
  76.         clients = [(address, lastmsg, status)]
  77.  
  78.  
  79. class ClientsList(wx.Frame):
  80.     """The List of the clients, in middle of the main window"""
  81.     def __init__(self, parent, id, title):
  82.         wx.Frame.__init__(self, parent, id, title, size=(700, 200))
  83.  
  84.         hbox = wx.BoxSizer(wx.HORIZONTAL)
  85.         panel = wx.Panel(self, -1)
  86.  
  87.         self.list = AutoWidthListCtrl(panel)
  88.         self.list.InsertColumn(0, 'Address', width=150)
  89.         self.list.InsertColumn(1, 'Last Message', width=450)
  90.         self.list.InsertColumn(2, 'Status', wx.LIST_FORMAT_RIGHT, 20)
  91.  
  92.         global clients
  93.         clients = [('N/A', 'N/A', 'N/A')]
  94.  
  95.         for i in clients:
  96.             index = self.list.InsertStringItem(sys.maxint, i[0])
  97.             self.list.SetStringItem(index, 1, i[1])
  98.             self.list.SetStringItem(index, 2, i[2])
  99.  
  100.         hbox.Add(self.list, 1, wx.EXPAND)
  101.         panel.SetSizer(hbox)
  102.  
  103.         self.Centre()
  104.         self.Show(True)
  105.  
  106.  
  107. class Window(wx.Frame):
  108.     """ The window class, with the events, the config, and the initialization of the window"""
  109.    
  110.     def __init__(self, *args, **kwargs):
  111.         super(Window, self).__init__(*args, **kwargs)
  112.  
  113.         self.InitUI()
  114.         self.SetSize((800,350))
  115.         self.SetTitle('BotNet')
  116.         self.Centre()
  117.         self.Show(True)
  118.  
  119.         #Initialization of the server
  120.         global serv
  121.         serv = Server()
  122.  
  123.     #Initialization of the main window
  124.     def InitUI(self):
  125.  
  126.         #Toolbar
  127.         self.toolbar = self.CreateToolBar()
  128.         tserver = self.toolbar.AddLabelTool(
  129.         wx.ID_ANY, '', wx.Bitmap('server.png'))
  130.  
  131.         self.toolbar.AddSeparator()
  132.  
  133.         tsettings = self.toolbar.AddLabelTool(
  134.         wx.ID_ANY, '', wx.Bitmap('settings.png'))
  135.         self.toolbar.Realize()
  136.  
  137.         #Panel
  138.         panel = wx.Panel(self)
  139.  
  140.         vbox = wx.BoxSizer(wx.VERTICAL)
  141.  
  142.         hbox1 = wx.BoxSizer(wx.HORIZONTAL)
  143.         listenBtn = wx.Button(panel, label='Listen', size=(100, 30))
  144.         stopBtn = wx.Button(panel, label='Stop', size=(100,30))
  145.         hbox1.Add(listenBtn)
  146.         hbox1.Add(stopBtn)
  147.         vbox.Add(hbox1, flag=wx.ALIGN_LEFT | wx.TOP | wx.LEFT, border=10)
  148.  
  149.         panel.SetSizer(vbox)
  150.  
  151.         #Clients list
  152.         ClientsList(None, -1, 'clients')
  153.  
  154.         #Binding
  155.         self.Bind(wx.EVT_BUTTON, self.onListenClick, listenBtn)
  156.         self.Bind(wx.EVT_BUTTON, self.onStopClick, stopBtn)
  157.  
  158.     #Events
  159.     def onQuit(self, e):
  160.         self.Close()
  161.  
  162.     def onListenClick(self, e):
  163.         serv.listen(serv.port, serv.serversocket)
  164.  
  165.     def onStopClick(self, e):
  166.         serv.clientsocket.shutdown(SHUT_RD | SHUT_WR)
  167.         serv.clientsocket.close()
  168.         print(red+"Connection Closed."+reset)
  169.  
  170.  
  171. #Main Function
  172. def main():
  173.  
  174.     app = wx.App()
  175.     Window(None)
  176.     app.MainLoop()
  177.  
  178.  
  179. if __name__ == '__main__':
  180.  
  181.     #We call the program
  182.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement