<commit_before># -*- coding: utf-8 -*-
##
##
## This file is part of CDS Indico.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Indico is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Indico is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

import sys
import xmlrpclib
import httplib


class TransportWithTimeout(xmlrpclib.Transport):

    def setTimeout(self, timeout):
        self._timeout = timeout

    def make_connection(self, host):
        return httplib.HTTPConnection(host, timeout=self._timeout)


class SafeTransportWithTimeout(xmlrpclib.SafeTransport):

    def setTimeout(self, timeout):
        self._timeout = timeout

    def make_connection(self, host):
        return httplib.HTTPSConnection(host, timeout=self._timeout)


def getServerWithTimeout(uri, transport=None, encoding=None, verbose=0,
                         allow_none=0, use_datetime=0, timeout=None):

    if uri.startswith("https://"):
        transport = SafeTransportWithTimeout()
    else:
        transport = TransportWithTimeout()
    transport.setTimeout(timeout)

    return xmlrpclib.ServerProxy(uri, transport, encoding, verbose, allow_none, use_datetime)
<commit_msg>[FIX] Fix problem with xmlrpclib and httplib<commit_after># -*- coding: utf-8 -*-
##
##
## This file is part of CDS Indico.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Indico is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Indico is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

import sys
import xmlrpclib
import httplib

class RequestConnection:

    def request(self, host, handler, request_body, verbose=0):
        # issue XML-RPC request

        h = self.make_connection(host)
        if verbose:
            h.set_debuglevel(1)
        try:
            self.send_request(h, handler, request_body)
            self.send_host(h, host)
            self.send_user_agent(h)
            self.send_content(h, request_body)

            response = h.getresponse(buffering=True)

            if response.status == 200:
                self.verbose = verbose
                return self.parse_response(response)
        except xmlrpclib.Fault:
            raise
        except Exception:
            # All unexpected errors leave connection in
            # a strange state, so we clear it.
            self.close()
            raise

        #discard any response data and raise exception
        if (response.getheader("content-length", 0)):
            response.read()
        raise xmlrpclib.ProtocolError(
            host + handler,
            response.status, response.reason,
            response.msg,
            )

class TransportWithTimeout(xmlrpclib.Transport, RequestConnection):

    def setTimeout(self, timeout):
        self._timeout = timeout

    def make_connection(self, host):
        return httplib.HTTPConnection(host, timeout=self._timeout)


class SafeTransportWithTimeout(xmlrpclib.SafeTransport, RequestConnection):

    def setTimeout(self, timeout):
        self._timeout = timeout

    def make_connection(self, host):
        return httplib.HTTPSConnection(host, timeout=self._timeout)


def getServerWithTimeout(uri, transport=None, encoding=None, verbose=0,
                         allow_none=0, use_datetime=0, timeout=None):

    if uri.startswith("https://"):
        transport = SafeTransportWithTimeout()
    else:
        transport = TransportWithTimeout()
    transport.setTimeout(timeout)

    return xmlrpclib.ServerProxy(uri, transport, encoding, verbose, allow_none, use_datetime)

{'fraction_non_alphanumeric': 0.08724262364678412, 'fraction_numerical': 0.01952876247081299, 'mean_word_length': 3.126094570928196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 11}, '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': '6421623', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1243, 'n_words': 454}