<commit_before>import json
import socket
from threading import Thread
import time
import random
import sys
import socks

class ProxySocket(object):

    def __init__(self, config, socksProxy):
        self.config = config
        if type(socksProxy) == int:
            self.host = config['defaultSocksHost']
            self.port = socksProxy
        else:
            self.host = socksProxy[0]
            self.port = socksProxy[1]
        self.failCount = 0
        self.fail = False

    def __cmp__(self, another):
        return self.failCount - another.failCount

    def apply(self, socksocket):
        socksocket.set_proxy(socks.SOCKS5, self.host, self.port)

class CheckProxies(Thread):

    def __init__(self, config):
        Thread.__init__(self)
        self.daemon = True
        self.config = config

    def run(self):
        while True:
            for socksProxy in self.config['socksProxies']:
                s = socks.socksocket()
                socksProxy.apply(s)
                s.settimeout(self.config['socketTimeout'])
                try:
                    s.connect(('www.google.com', 80))
                    s.send(bytearray(b'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'))
                    if s.recv(65536) == '':
                        raise Exception()
                    socksProxy.fail = False
                except Exception:
                    socksProxy.failCount += 1
                    socksProxy.fail = True
            time.sleep(self.config['checkInterval'])

class ProxySelector(object):

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

    def __call__(self):
        goodProxies = [socksProxy for socksProxy in self.config['socksProxies'] if not socksProxy.fail]
        goodProxies.sort()
        return goodProxies[int(random.randint(0, len(goodProxies)) * random.randint(0, len(goodProxies)) / len(goodProxies)) - 1]

class Pipe(Thread):

    def __init__(self):
        Thread.__init__(self)

    def setSockPair(self, sockIn, sockOut):
        self.sockIn = sockIn
        self.sockOut = sockOut

    def pipeData(self):
        try:
            while True:
                self.sockOut.send(self.sockIn.recv(65536))
        except Exception:
            self.sockIn.close()
            self.sockOut.close()

    def run(self):
        self.pipeData()

class Tunnel(Pipe):

    def __init__(self, config, sock, hosts, proxySelector):
        Pipe.__init__(self)
        self.config = config
        self.sock = sock
        self.hosts = hosts
        self.proxySelector = proxySelector

    def run(self):
        proxySock = socks.socksocket()
        self.proxySelector().apply(proxySock)
        host = self.hosts[random.randint(0, len(self.hosts) - 1)]
        proxySock.connect((host[0], host[1]))
        pipe = Pipe()
        pipe.setSockPair(proxySock, self.sock)
        pipe.start()
        self.setSockPair(self.sock, proxySock)
        self.pipeData()

class Server(Thread):

    def __init__(self, config, port, hosts, proxySelector):<commit_msg>Fix infinite loop bug<commit_after>import json
import socket
from threading import Thread
import time
import random
import sys
import socks

class ProxySocket(object):

    def __init__(self, config, socksProxy):
        self.config = config
        if type(socksProxy) == int:
            self.host = config['defaultSocksHost']
            self.port = socksProxy
        else:
            self.host = socksProxy[0]
            self.port = socksProxy[1]
        self.failCount = 0
        self.fail = False

    def __cmp__(self, another):
        return self.failCount - another.failCount

    def apply(self, socksocket):
        socksocket.set_proxy(socks.SOCKS5, self.host, self.port)

class CheckProxies(Thread):

    def __init__(self, config):
        Thread.__init__(self)
        self.daemon = True
        self.config = config

    def run(self):
        while True:
            for socksProxy in self.config['socksProxies']:
                s = socks.socksocket()
                socksProxy.apply(s)
                s.settimeout(self.config['socketTimeout'])
                try:
                    s.connect(('www.google.com', 80))
                    s.send(bytearray(b'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'))
                    if s.recv(65536) == '':
                        raise Exception()
                    socksProxy.fail = False
                except Exception:
                    socksProxy.failCount += 1
                    socksProxy.fail = True
            time.sleep(self.config['checkInterval'])

class ProxySelector(object):

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

    def __call__(self):
        goodProxies = [socksProxy for socksProxy in self.config['socksProxies'] if not socksProxy.fail]
        goodProxies.sort()
        return goodProxies[int(random.randint(0, len(goodProxies)) * random.randint(0, len(goodProxies)) / len(goodProxies)) - 1]

class Pipe(Thread):

    def __init__(self):
        Thread.__init__(self)

    def setSockPair(self, sockIn, sockOut):
        self.sockIn = sockIn
        self.sockOut = sockOut

    def pipeData(self):
        try:
            while True:
                data = self.sockIn.recv(65536)
                if data == '':
                    break
                self.sockOut.send(data)
        finally:
            self.sockIn.close()
            self.sockOut.close()

    def run(self):
        self.pipeData()

class Tunnel(Pipe):

    def __init__(self, config, sock, hosts, proxySelector):
        Pipe.__init__(self)
        self.config = config
        self.sock = sock
        self.hosts = hosts
        self.proxySelector = proxySelector

    def run(self):
        proxySock = socks.socksocket()
        self.proxySelector().apply(proxySock)
        host = self.hosts[random.randint(0, len(self.hosts) - 1)]
        proxySock.connect((host[0], host[1]))
        pipe = Pipe()
        pipe.setSockPair(proxySock, self.sock)
        pipe.start()
        self.setSockPair(self.sock, proxySock)
        self.pipeData()

class Server(Thread):

    def __init__(self, config, port, hosts, proxySelector):
{'fraction_non_alphanumeric': 0.10765239948119326, 'fraction_numerical': 0.008430609597924773, 'mean_word_length': 1.9701492537313432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5402847', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1781, 'n_words': 384}