<commit_before>#!/usr/bin/python
import socket               # Import socket module
import sys
import threading
import select


class Connector:

    def __init__(self, host='localhost', port=5000):
        self.host = host
        self.port = port
        self.skt = socket.socket()
        self.is_connected = False
        self.request_shutdown = False
        self.connect()

    def fileno(self):
        """Return socket file number.
        Interface required by select().
        """
        return self.skt.fileno()

    def connect(self):
        try:
            self.skt.connect((self.host, self.port))
            self.is_connected = True
        except socket.error as socketerror:
            print 'error to connect server: {0}'.format(self.host)
            raise socketerror 

    def getmsg(self):
        backstr = self.skt.recv(1024)
        if backstr:
            return backstr

    def disconnect(self):
        self.is_connected = False
        self.skt.close()

    def registerApp(self, app):
        self.app = app

    def serve_forever(self, poll_interval=0.5):
        """Monitor message coming from the socket

        Polls for shutdown every poll_interval seconds. Ignores
        self.timeout. If you need to do periodic tasks, do them in
        another thread.
        """
        try:
            while not self.request_shutdown:
                r, w, e = select.select([self], [], [], poll_interval)
                if self in r:
                    self.handle_message()
        finally:
            self.disconnect()

    def handle_message(self):
        self.app.update_msg(getmsg())

if __name__ == '__main__':
    c = Connector()
    c.getmsg()<commit_msg>add new appconnetor class to handle app message<commit_after>#!/usr/bin/python
import socket               # Import socket module
import sys
import threading
import select


class Connector:

    def __init__(self, host='localhost', port=5000):
        self.host = host
        self.port = port
        self.skt = socket.socket()
        self.is_connected = False
        self.request_shutdown = False
        self.connect()

    def fileno(self):
        """Return socket file number.
        Interface required by select().
        """
        return self.skt.fileno()

    def connect(self):
        try:
            self.skt.connect((self.host, self.port))
            self.is_connected = True
        except socket.error as socketerror:
            print 'error to connect server: {0}'.format(self.host)
            raise socketerror 

    def getmsg(self):
        backstr = self.skt.recv(1024)
        if backstr:
            return backstr

    def disconnect(self):
        self.is_connected = False
        self.skt.close()

    def serve_forever(self, poll_interval=0.5):
        """Monitor message coming from the socket

        Polls for shutdown every poll_interval seconds. Ignores
        self.timeout. If you need to do periodic tasks, do them in
        another thread.
        """
        try:
            while not self.request_shutdown:
                r, w, e = select.select([self], [], [], poll_interval)
                if self in r:
                    self.handle_message()
        finally:
            self.disconnect()

    def handle_message(self):
        """overwrite by sub class
        consume the message
        """
        pass


class AppConnector(Connector):
    """App specified connector
    pass message to app
    """
    def __init__(self, host, port, app=None):
        Connector.__init__(self, host, port)
        self.app = app

    def handle_message(self):
        self.app.update_msg(self.getmsg())


if __name__ == '__main__':
    c = Connector()
    c.getmsg()
{'fraction_non_alphanumeric': 0.09607104413347686, 'fraction_numerical': 0.005920344456404736, 'mean_word_length': 1.9107282693813625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4348253', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1015, 'n_words': 294}