<commit_before>import socket
import logging
from traceback import format_exc

from websocket import WebSocket
from exceptions import InvalidRequest


class Server(object):
    def __init__(self, port, address='', log_level=logging.INFO, protocols=[]):
        logging.basicConfig(level=log_level,
                format='%(asctime)s: %(levelname)s: %(message)s',
                datefmt='%H:%M:%S')

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        logging.info('Starting server at %s:%d', address, port)
        self.sock.bind((address, port))
        self.sock.listen(5)

        self.clients = []
        self.protocols = protocols

    def run(self):
        while True:
            try:
                client_socket, address = self.sock.accept()
                client = Client(self, client_socket, address)
                client.handshake()
                self.clients.append(client)
                logging.info('Registered client %s', client)
                client.run_threaded()
            except InvalidRequest as e:
                logging.error('Invalid request: %s', e.message)
            except KeyboardInterrupt:
                logging.info('Received interrupt, stopping server...')
                break
            except Exception as e:
                logging.error(format_exc(e))

    def onopen(self, client):
        """
        Called when a new client connects.
        """
        pass

    def onmessage(self, client, message):
        """
        Called when a message is received from some client. `message' is a
        Message object
        """
        return NotImplemented

    def onclose(self, client):
        """
        Called when a client disconnects.
        """
        pass


class Client(WebSocket):
    def __init__(self, server, sock, address):
        super(Client, self).__init__(sock, address)
        self.server = server

    def onopen(self):
        self.server.onopen(self)

    def onmessage(self, message):
        self.server.onmessage(self, message)

    def onclose(self):
        self.server.onclose(self)

    def __str__(self):
        return '<Client at %s:%d>' % self.address


if __name__ == '__main__':
    import sys
    port = int(sys.argv[1]) if len(sys.argv) > 1 else 80
    Server(port=port, log_level=logging.DEBUG).run()<commit_msg>Default handlers of Client now log events<commit_after>import socket
import logging
from traceback import format_exc

from websocket import WebSocket
from exceptions import InvalidRequest


class Server(object):
    def __init__(self, port, address='', log_level=logging.INFO, protocols=[]):
        logging.basicConfig(level=log_level,
                format='%(asctime)s: %(levelname)s: %(message)s',
                datefmt='%H:%M:%S')

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        logging.info('Starting server at %s:%d', address, port)
        self.sock.bind((address, port))
        self.sock.listen(5)

        self.clients = []
        self.protocols = protocols

    def run(self):
        while True:
            try:
                client_socket, address = self.sock.accept()
                client = Client(self, client_socket, address)
                client.handshake()
                self.clients.append(client)
                logging.info('Registered client %s', client)
                client.run_threaded()
            except InvalidRequest as e:
                logging.error('Invalid request: %s', e.message)
            except KeyboardInterrupt:
                logging.info('Received interrupt, stopping server...')
                break
            except Exception as e:
                logging.error(format_exc(e))

    def onopen(self, client):
        """
        Called after `client' has completed its handshake.
        """
        logging.debug('Opened socket to client %s' % client)

    def onmessage(self, client, message):
        """
        Called when Message object `message' is received from `client'.
        """
        logging.debug('Received %s from %s' % (message, client))

    def onclose(self, client):
        """
        Called when `client' disconnects.
        """
        logging.debug('Closed socket to client %s' % client)


class Client(WebSocket):
    def __init__(self, server, sock, address):
        super(Client, self).__init__(sock, address)
        self.server = server

    def onopen(self):
        self.server.onopen(self)

    def onmessage(self, message):
        self.server.onmessage(self, message)

    def onping(self, payload):
        logging.debug('Sent ping "%s" to %s' % (payload, self))

    def onpong(self, payload):
        logging.debug('Received pong "%s" from %s' % (payload, self))

    def onclose(self):
        self.server.onclose(self)

    def __str__(self):
        return '<Client at %s:%d>' % self.address


if __name__ == '__main__':
    import sys
    port = int(sys.argv[1]) if len(sys.argv) > 1 else 80
    Server(port, log_level=logging.DEBUG).run()
{'fraction_non_alphanumeric': 0.11455108359133127, 'fraction_numerical': 0.0023219814241486067, 'mean_word_length': 2.2427854454203264, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3155715', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1433, 'n_words': 372}