<commit_before># (c) <NAME>, Nelen & Schuurmans. MIT licensed, see LICENSE.rst.
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from datetime import datetime
import SocketServer
import logging.config
import os

import psycopg2
import pytz

from ddsc_socket import settings
from ddsc_socket.celery import celery

logger = logging.getLogger(__name__)

BUFSIZE = 1024  # bytes
SQL = "SELECT count(1) FROM lizard_nxt_ipmapping WHERE ip_address = '{}';"


class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):

        ip, port = self.client_address
        logger.info("Client connecting from %s:%d.", ip, port)

        try:
            conn, cur = None, None
            conn = psycopg2.connect(**settings.DATABASE)
            cur = conn.cursor()
            cur.execute(SQL.format(ip))
            count = cur.fetchone()[0]
        except Exception as e:
            logger.error(e)
            return
        finally:
            if cur and not cur.closed:
                cur.close()
            if conn and not conn.closed:
                conn.close()

        if count < 1:
            logger.warning("Will not serve %s (unauthorized).", ip)
            return

        self.request.send("ok")
        data = self.request.recv(BUFSIZE)  # BLOCKING

        if not data:
            logger.info("Connection with %s lost.", ip)
            return

        utc = datetime.now(pytz.utc).isoformat()
        filename = "{}_{}.csv".format(ip, utc)
        file_path = os.path.join(settings.DIR, filename)

        # TODO: implement a timeout.
        # TODO: implement a rolling file.

        with open(filename, 'wb') as f:
            f.write(data)
            while True:
                self.request.send("ok")<commit_msg>Use the full path instead of just the filename.<commit_after># (c) <NAME>, Nelen & Schuurmans. MIT licensed, see LICENSE.rst.
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from datetime import datetime
import SocketServer
import logging.config
import os

import psycopg2
import pytz

from ddsc_socket import settings
from ddsc_socket.celery import celery

logger = logging.getLogger(__name__)

BUFSIZE = 1024  # bytes
SQL = "SELECT count(1) FROM lizard_nxt_ipmapping WHERE ip_address = '{}';"


class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):

        ip, port = self.client_address
        logger.info("Client connecting from %s:%d.", ip, port)

        try:
            conn, cur = None, None
            conn = psycopg2.connect(**settings.DATABASE)
            cur = conn.cursor()
            cur.execute(SQL.format(ip))
            count = cur.fetchone()[0]
        except Exception as e:
            logger.error(e)
            return
        finally:
            if cur and not cur.closed:
                cur.close()
            if conn and not conn.closed:
                conn.close()

        if count < 1:
            logger.warning("Will not serve %s (unauthorized).", ip)
            return

        self.request.send("ok")
        data = self.request.recv(BUFSIZE)  # BLOCKING

        if not data:
            logger.info("Connection with %s lost.", ip)
            return

        utc = datetime.now(pytz.utc).isoformat()
        filename = "{}_{}.csv".format(ip, utc)
        file_path = os.path.join(settings.DIR, filename)

        # TODO: implement a timeout.
        # TODO: implement a rolling file.

        with open(file_path, 'wb') as f:
            f.write(data)
            while True:
                self.request.send("ok")
{'fraction_non_alphanumeric': 0.09839937024403043, 'fraction_numerical': 0.005247966413014957, 'mean_word_length': 2.1819699499165277, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1977908', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1066, 'n_words': 315}