<commit_before>import asyncio
import socket
import uvloop

from .parsers import HttpParser


class HttpReader(object):
    __slots__ = ['reader']

    BUFFER_SIZE = 102400  # 100kb

    def __init__(self, reader):
        self.reader = reader

    async def get_one(self):
        http_parser = HttpParser()
        env = dict()
        while not http_parser.complete:
            data = await self.reader.read(self.BUFFER_SIZE)
            if not data:
                return None
            http_parser.feed(data, env)
        return env


class NestResponder(object):
    __slots__ = ['writer', 'app']

    def __init__(self, writer, app):
        self.writer = writer
        self.app = app

    def start_response(self, status, headers):
        self.writer.write(b'HTTP/1.1 ')
        self.writer.write(status.encode('ascii'))
        self.writer.write(b'\r\n')
        for h in headers:
            self.writer.write(': '.join(h).encode('ascii'))
            self.writer.write(b'\r\n')
        self.writer.write(b'\r\n')

    async def respond(self, env):
        res = self.app(env, self.start_response)
        for data in res:
            self.writer.write(data)
            await self.writer.drain()


class Nest(object):
    def __init__(self, host='0.0.0.0', port=8000, app=None):
        self._clients = {}
        self.sock = socket.socket()
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((host, port))
        self.app = app

    def accept(self, reader, writer):
        task = self.loop.create_task(self.handle(reader, writer))
        self._clients[task] = (reader, writer)

        def client_done(task):
            del self._clients[task]
            writer.close()
        task.add_done_callback(client_done)

    async def handle(self, reader, writer):
        http = HttpReader(reader)
        responder = NestResponder(writer, self.app)
        while True:
            env = await http.get_one()
            if env is None:
                break
            await responder.respond(env)

    def run(self):
        self.loop = uvloop.new_event_loop()
        server_coro = asyncio.start_server(
            self.accept, sock=self.sock, backlog=1000, loop=self.loop)
        server = self.loop.run_until_complete(server_coro)
        try:
            self.loop.run_forever()
        finally:
            server.close()<commit_msg>Handling keyboard interrupt in run.<commit_after>import asyncio
import socket
import uvloop

from .parsers import HttpParser


class HttpReader(object):
    __slots__ = ['reader']

    BUFFER_SIZE = 102400  # 100kb

    def __init__(self, reader):
        self.reader = reader

    async def get_one(self):
        http_parser = HttpParser()
        env = dict()
        while not http_parser.complete:
            data = await self.reader.read(self.BUFFER_SIZE)
            if not data:
                return None
            http_parser.feed(data, env)
        return env


class NestResponder(object):
    __slots__ = ['writer', 'app']

    def __init__(self, writer, app):
        self.writer = writer
        self.app = app

    def start_response(self, status, headers):
        self.writer.write(b'HTTP/1.1 ')
        self.writer.write(status.encode('ascii'))
        self.writer.write(b'\r\n')
        for h in headers:
            self.writer.write(': '.join(h).encode('ascii'))
            self.writer.write(b'\r\n')
        self.writer.write(b'\r\n')

    async def respond(self, env):
        res = self.app(env, self.start_response)
        for data in res:
            self.writer.write(data)
            await self.writer.drain()


class Nest(object):
    def __init__(self, host='0.0.0.0', port=8000, app=None):
        self._clients = {}
        self.sock = socket.socket()
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((host, port))
        self.app = app

    def accept(self, reader, writer):
        task = self.loop.create_task(self.handle(reader, writer))
        self._clients[task] = (reader, writer)

        def client_done(task):
            del self._clients[task]
            writer.close()
        task.add_done_callback(client_done)

    async def handle(self, reader, writer):
        http = HttpReader(reader)
        responder = NestResponder(writer, self.app)
        while True:
            env = await http.get_one()
            if env is None:
                break
            await responder.respond(env)

    def run(self):
        self.loop = uvloop.new_event_loop()
        server_coro = asyncio.start_server(
            self.accept, sock=self.sock, backlog=1000, loop=self.loop)
        server = self.loop.run_until_complete(server_coro)
        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            print('Shutting down...')
        finally:
            server.close()
{'fraction_non_alphanumeric': 0.11444308445532436, 'fraction_numerical': 0.009791921664626682, 'mean_word_length': 2.0855884203901827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3892394', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1468, 'n_words': 321}