<commit_before>import socket
import traceback
from .Request import Request


class HttpServer:
    """
    A simple, single threaded, http server.
    """
    def __init__(self, host='localhost', port=4001):
        self._on_request_handler = None
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._socket.bind((host, port))
        self._conn = None
        self._address = None
        self._run = True

    def start(self):
        """
        Starts the server.
        """
        print('HTTP Start')
        self._socket.listen(1)

        while self._run:
            self._conn, self._address = self._socket.accept()
            request = self._recv_all()
            request = self._parse_request(request)
            response = self._on_request_handler.on_request(request)
            self._conn.sendall(bytes(self._build_response(response), 'utf-8'))
            self._conn.close()

    def stop(self):
        """
        Stops the server.
        """
        print('HTTP Stop')
        self._run = False
        try:
            if self._conn is not None:
                self._conn.close()
            self._socket.shutdown(socket.SHUT_RDWR)
            self._socket.close()
            print('HTTP Stopped')
        except OSError as e:
            print('Skipped OSError')
            print(str(e))
            print(traceback.format_exc())

    def _recv_all(self):
        """
        Receive all data.
        """
        msg = bytearray()
        while True:
            chunk = self._conn.recv(4096)
            msg.extend(chunk)
<commit_msg>Close server when the plugin is disabled/reloaded #20<commit_after>import socket
import traceback
from .Request import Request


class HttpServer:
    """
    A simple, single threaded, http server.
    """
    def __init__(self, host='localhost', port=4001):
        self._on_request_handler = None
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._socket.bind((host, port))
        self._conn = None
        self._address = None
        self._run = True

    def start(self):
        """
        Starts the server.
        """
        print('HTTP Start')
        self._socket.listen(1)

        while self._run:
            self._conn, self._address = self._socket.accept()
            request = self._recv_all()
            request = self._parse_request(request)
            response = self._on_request_handler.on_request(request)
            self._conn.sendall(bytes(self._build_response(response), 'utf-8'))
            self._conn.close()

    def stop(self):
        """
        Stops the server.
        """
        print('HTTP Stop')
        self._run = False

        if self._conn is not None:
            try:
                self._conn.close()
            except OSError as e:
                print(str(e))
                print(traceback.format_exc())
                pass

        try:
            self._socket.shutdown(socket.SHUT_RDWR)
        except OSError as e:
            print(str(e))
            print(traceback.format_exc())
            pass

        try:
            self._socket.close()
        except OSError as e:
            print(str(e))
            print(traceback.format_exc())
            pass
        print('HTTP Stopped')

    def _recv_all(self):
        """
        Receive all data.
        """
        msg = bytearray()
        while True:
            chunk = self._conn.recv(4096)
            msg.extend(chunk)

{'fraction_non_alphanumeric': 0.10668133076711575, 'fraction_numerical': 0.006598845202089634, 'mean_word_length': 1.7169529499626588, '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': '5958993', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1013, 'n_words': 218}