def __init__(self, websocket, path=None, server=None): self.ws = websocket self.path = path self.server = server self.username = '' self.log = logger.getChild('Listener{0}{1}'.format( self.username, self.ws.remote_address))
def __init__(self, websocket, path=None, server=None): self.ws = websocket self.path = path self.server = server self.ps = subscribe('BoardCast') self.exit = 0 self.log = logger.getChild('Sender{0}'.format(self.ws.remote_address))
def __init__(self): self.db_name = 'Server.db' self.db_path = os.path.join(DB_ROOT, self.db_name) self.log = logger.getChild('Database') self.databaseInitial()
def __init__(self): self.sender = None self.listener = None self.websocket = None self.path = None self.log = logger.getChild('Server')
import json from Server.util import logger log = logger.getChild('MessageClass') class BaseMessage: def __init__(self, data=None): self._init_type() self.log = log.getChild(self.type) self.log.debug(self.__dict__) self._parse(data) self._check_data() def _init_type(self): self.type = 'BaseMessage' def _parse_dict(self, data): self.source = data.get('source') if data.get('type') is not None: self.type = data.get('type') def _parse(self, data=None): if data is not None: if isinstance(data, str): self._parse_dict(json.loads(data)) elif isinstance(data, dict): self._parse_dict(data) else: self.log.error('Wrong type with data: {0}'.format(data))
import asyncio import json from Server.core import Route, send, crypto from Server.message import * from Server.util import logger from Server.database import db route_log = logger.getChild('Route') @Route.route('AuthRequestMessage') async def login(message, ws): log = route_log.getChild('Login') message = AuthRequestMessage(message) log.debug(message) if db.checkUser(message.username, message.password): await ws.send( AuthResultMessage({ 'status': 'Logged', 'username': message.username }).to_json()) return {'status': 'Logged', 'channel': message.username} else: await ws.send(AuthResultMessage({'status': 'Failed'}).to_json()) return {'status': 'Failed'} @Route.route('RegisterRequestMessage') async def register(message, ws): log = route_log.getChild('Register')
import os import redis from Server.util import logger log = logger.getChild('Redis') REDIS_HOST = os.environ.get('REDIS_HOST') if not REDIS_HOST: REDIS_HOST = 'rickyhao.com' redis_pool = redis.ConnectionPool(host=REDIS_HOST, port=6379) def subscribe(channel): redis_connection = redis.StrictRedis(connection_pool=redis_pool) pubsub = redis_connection.pubsub() pubsub.subscribe(channel) log.debug('Subscribe channel: {0}'.format(channel)) return pubsub def send(channel, message): log.debug('Channel: {0}, Message: {1}'.format(channel, message)) redis_connection = redis.StrictRedis(connection_pool=redis_pool) return redis_connection.publish(channel, message)