Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. from tornado import websocket, web, ioloop
  2. from modules import account
  3. import protocols
  4. import json
  5. import time
  6. import re
  7. import sys
  8.  
  9.  
  10. bannedip = {}
  11. clients = {}
  12. rooms = {}
  13.  
  14. middlewares = ['module.log'] # not implemented yet
  15.  
  16. class Server():
  17.    
  18.     @staticmethod
  19.     def removeFromClients(socket):
  20.         if socket in clients:
  21.             print("We are removing %s from client list"%clients[socket].nickname);
  22.             del clients[socket]
  23.             print(clients)
  24.  
  25. class WebsocketClient():
  26.  
  27.     logged_in = False
  28.     nickname = None
  29.     socket = None
  30.  
  31.     def __init__(self, socket):
  32.         self.socket = socket;
  33.  
  34. class SocketHandler(websocket.WebSocketHandler):
  35.  
  36.     def check_origin(self, origin):
  37.         return True
  38.  
  39.     def open(self):
  40.         if self not in clients:
  41.             cl = WebsocketClient(self)
  42.             clients[self]=cl
  43.  
  44.     def on_close(self):
  45.         if self in clients:
  46.             print("Client %s left chatroom"%(clients[self].nickname))
  47.             Server.removeFromClients(self)
  48.  
  49.     def on_message(self,message):
  50.         handled = False
  51.         data = None
  52.         try:
  53.             data = json.loads(message)
  54.             print(data)
  55.         except json.JSONDecodeError as e:
  56.             print(e.msg)
  57.             self.close()
  58.             return Server.removeFromClients(self)
  59.  
  60.  
  61.         #       .Data Classifer(int)  .Function name(string)  .All other required params
  62.         #                                                     .required by protocol
  63.  
  64.         # data [0                  ,  function              , more params]
  65.         print(data[0])
  66.         dataClassification = protocols.DataClassifier[data[0]] \
  67.                              if data[0] in protocols.DataClassifier else None
  68.  
  69.         print(dataClassification)
  70.         if self in clients:
  71.             for name, module in sys.modules.items():
  72.                 if not name.startswith("modules."):
  73.                     continue
  74.                 fn = getattr(module, dataClassification)
  75.                 if(callable(fn)):
  76.                     tempHandle = fn(self, data[1::], globals())
  77.                     if(tempHandle == True):
  78.                         return
  79.                 # module.globals()['']
  80.                 # self.write_message(mssage)
  81.  
  82.         if handled == False:
  83.             self.close()
  84.             Server.removeFromClients(self)
  85.  
  86. app = web.Application([
  87.     (r'/', SocketHandler),
  88. ])
  89.  
  90. if __name__ == '__main__':
  91.     print(protocols.DataClassifier)
  92.     app.listen(5000)
  93.     ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement