<commit_before>#!/usr/bin/python
import threading
import bson
from SocketServer import ThreadingMixIn, TCPServer, BaseRequestHandler

from wsgi import WSGIHandler, Environ

class Server(ThreadingMixIn, TCPServer):
    def __init__(self, addr, handler, app):
        self.app = app
        self.connected_handlers = []
        TCPServer.__init__(self, addr, handler)

    @classmethod
    def run_server(cls, addr, app):
        bson.patch_socket()
        server = cls(addr, WSGITRequestHandler, app)
        thread = threading.Thread(target=server.serve_forever)
        thread.start()
        return server, thread

class WSGITRequestHandler(BaseRequestHandler):
    def setup(self):
        self.conn = self.request
        self.server.connected_handlers.append(self)

    def handle(self):
        while True:
            try:
                obj = self.conn.recvobj()
            except Exception:
                logger.debug('Bad Request. Not bson protocol.')
            if obj == None:
                break
            environ = self._get_environ(obj)
            wsgi_handler = WSGIHandler()
            obj = wsgi_handler.call_application(self.server.app, 
                                                environ.get_dict())
            self.conn.send(obj)

    def _get_environ(self, obj):
        obj.update(dict(server_name=self.conn.getsockname()[0],
                        server_port=self.conn.getsockname()[1],
                        remote_addr=self.client_address[0],
                        remote_port=self.client_address[1]))
        return Environ(obj)


    def finish(self):
        self.conn.close()

if __name__ == '__main__':
    import sys, os, time
    sys.path.append(os.getcwd())
    if len(sys.argv) != 3:
        print 'Usage : %s BIND_IP:PORT MODULENAME.APPLICATION_NAME'%sys.argv[0]
        sys.exit(1)
    path = sys.argv[2].split('.')
    name = path[-1]
    module = __import__('.'.join(path[:-1]), fromlist=[name])
    ip,port = sys.argv[1].split(':')
    try:
        server, thread= Server.run_server((ip,int(port)), getattr(module, name))
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        server.shutdown()<commit_msg>Meta data omitted bug fixed<commit_after>#!/usr/bin/python
import threading
import bson
from SocketServer import ThreadingMixIn, TCPServer, BaseRequestHandler

from wsgi import WSGIHandler, Environ

class Server(ThreadingMixIn, TCPServer):
    def __init__(self, addr, handler, app):
        self.app = app
        self.connected_handlers = []
        TCPServer.__init__(self, addr, handler)

    @classmethod
    def run_server(cls, addr, app):
        bson.patch_socket()
        server = cls(addr, WSGITRequestHandler, app)
        thread = threading.Thread(target=server.serve_forever)
        thread.start()
        return server, thread

class WSGITRequestHandler(BaseRequestHandler):
    def setup(self):
        self.conn = self.request
        self.server.connected_handlers.append(self)

    def handle(self):
        while True:
            try:
                obj = self.conn.recvobj()
            except Exception:
                logger.debug('Bad Request. Not bson protocol.')
            if obj == None:
                break
            environ = self._get_environ(obj)
            wsgi_handler = WSGIHandler()
            obj = wsgi_handler.call_application(self.server.app, 
                                                environ.get_dict())
            self.conn.send(obj)

    def _get_environ(self, obj):
        meta = (dict(server_name=self.conn.getsockname()[0],
                        server_port=self.conn.getsockname()[1],
                        remote_addr=self.client_address[0],
                        remote_port=self.client_address[1]))
        parameters = obj
        return Environ(dict(meta=meta, parameters=parameters))


    def finish(self):
        self.conn.close()

if __name__ == '__main__':
    import sys, os, time
    sys.path.append(os.getcwd())
    if len(sys.argv) != 3:
        print 'Usage : %s BIND_IP:PORT MODULENAME.APPLICATION_NAME'%sys.argv[0]
        sys.exit(1)
    path = sys.argv[2].split('.')
    name = path[-1]
    module = __import__('.'.join(path[:-1]), fromlist=[name])
    ip,port = sys.argv[1].split(':')
    try:
        server, thread= Server.run_server((ip,int(port)), getattr(module, name))
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        server.shutdown()
{'fraction_non_alphanumeric': 0.11456701722980532, 'fraction_numerical': 0.005370328932647125, 'mean_word_length': 2.192857142857143, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '59215', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1320, 'n_words': 265}