Python httplib2.ProxyInfo() Examples

The following are 19 code examples of httplib2.ProxyInfo(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib2 , or try the search function .
Example #1
Source File: gmail.py    From iris-relay with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, config=None, proxy_config=None):
        self.config = config
        self.client = None
        if proxy_config and 'host' in proxy_config and 'port' in proxy_config:
            proxy_info = ProxyInfo(socks.PROXY_TYPE_HTTP_NO_TUNNEL,
                                   proxy_config['host'], proxy_config['port'])
        else:
            proxy_info = None
        self.http = Http(proxy_info=proxy_info)
        self.var_dir = self.config['var_dir']
        if not exists(self.var_dir):
            makedirs(self.var_dir)
        self.history_id_f = join(self.var_dir, 'gmail_last_history_id')
        if exists(self.history_id_f):
            with open(self.history_id_f) as fh:
                logger.info('Loaded last gmail history id %d', int(fh.read()))
        else:
            # store an invalid id, which will get renewed on next push event
            self.save_last_history_id('1') 
Example #2
Source File: GoogleCloudFunctions.py    From content with MIT License 6 votes vote down vote up
def get_http_client_with_proxy(proxy, insecure):
        """
        Create an http client with proxy with whom to use when using a proxy.
        :param proxy: Whether to use a proxy.
        :param insecure: Whether to disable ssl and use an insecure connection.
        :return:
        """
        if proxy:
            proxies = handle_proxy()
            https_proxy = proxies.get('https')
            http_proxy = proxies.get('http')
            proxy_conf = https_proxy if https_proxy else http_proxy
            # if no proxy_conf - ignore proxy
            if proxy_conf:
                if not proxy_conf.startswith('https') and not proxy_conf.startswith('http'):
                    proxy_conf = 'https://' + proxy_conf
                parsed_proxy = urllib.parse.urlparse(proxy_conf)
                proxy_info = httplib2.ProxyInfo(
                    proxy_type=httplib2.socks.PROXY_TYPE_HTTP,
                    proxy_host=parsed_proxy.hostname,
                    proxy_port=parsed_proxy.port,
                    proxy_user=parsed_proxy.username,
                    proxy_pass=parsed_proxy.password)
                return httplib2.Http(proxy_info=proxy_info, disable_ssl_certificate_validation=insecure)
        return httplib2.Http(disable_ssl_certificate_validation=insecure)
    # disable-secrets-detection-end 
Example #3
Source File: oauth2_client.py    From gcs-oauth2-boto-plugin with Apache License 2.0 6 votes vote down vote up
def __init__(self, cache_key_base, access_token_cache=None,
               datetime_strategy=datetime.datetime, auth_uri=None,
               token_uri=None, disable_ssl_certificate_validation=False,
               proxy_host=None, proxy_port=None, proxy_user=None,
               proxy_pass=None, ca_certs_file=None):
    # datetime_strategy is used to invoke utcnow() on; it is injected into the
    # constructor for unit testing purposes.
    self.auth_uri = auth_uri
    self.token_uri = token_uri
    self.cache_key_base = cache_key_base
    self.datetime_strategy = datetime_strategy
    self.access_token_cache = access_token_cache or InMemoryTokenCache()
    self.disable_ssl_certificate_validation = disable_ssl_certificate_validation
    self.ca_certs_file = ca_certs_file
    if proxy_host and proxy_port:
      self._proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP,
                                            proxy_host,
                                            proxy_port,
                                            proxy_user=proxy_user,
                                            proxy_pass=proxy_pass,
                                            proxy_rdns=True)
    else:
      self._proxy_info = None 
Example #4
Source File: http.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 6 votes vote down vote up
def _prepare_proxy_info(self, proxy):
        if not proxy or not proxy.enabled:
            _logger.debug('Proxy is not enabled')
            return None

        username = proxy.username \
            if 'username' in proxy and proxy.username else None
        password = proxy.password \
            if 'password' in proxy and proxy.password else None

        proxy_type = self._PROXY_TYPE.get(proxy.type) or self._PROXY_TYPE['http']

        return ProxyInfo(proxy_host=proxy.host,
                         proxy_port=int(proxy.port),
                         proxy_type=proxy_type,
                         proxy_user=username,
                         proxy_pass=password,
                         proxy_rdns=proxy.rdns) 
Example #5
Source File: test_proxies.py    From alfred-gmail with MIT License 5 votes vote down vote up
def testSimpleProxy(self):
        proxy_info = httplib2.ProxyInfo(
            socks.PROXY_TYPE_HTTP, "localhost", self.proxyport
        )
        client = httplib2.Http(proxy_info=proxy_info)
        src = "miniserver.py"
        response, body = client.request("http://localhost:%d/%s" % (self.port, src))
        self.assertEqual(response.status, 200)
        self.assertEqual(body, open(os.path.join(miniserver.HERE, src)).read())
        lf = open(self.logfile).read()
        expect = 'Established connection to host "127.0.0.1" ' "using file descriptor"
        self.assertTrue(
            expect in lf, "tinyproxy did not proxy a request for miniserver"
        ) 
Example #6
Source File: test_no_socket.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def testProxyDisabled(self):
        proxy_info = httplib2.ProxyInfo('blah',
                                        'localhost', 0)
        client = httplib2.Http(proxy_info=proxy_info)
        self.assertRaises(httplib2.ProxiesUnavailableError,
                          client.request, 'http://localhost:-1/') 
Example #7
Source File: test_proxies.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def testSimpleProxy(self):
        proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP,
                                        'localhost', self.proxyport)
        client = httplib2.Http(proxy_info=proxy_info)
        src = 'miniserver.py'
        response, body = client.request('http://localhost:%d/%s' %
                                        (self.port, src))
        self.assertEqual(response.status, 200)
        self.assertEqual(body, open(os.path.join(miniserver.HERE, src)).read())
        lf = open(self.logfile).read()
        expect = ('Established connection to host "127.0.0.1" '
                  'using file descriptor')
        self.assertTrue(expect in lf,
                        'tinyproxy did not proxy a request for miniserver') 
Example #8
Source File: test_no_socket.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def testProxyDisabled(self):
        proxy_info = httplib2.ProxyInfo('blah',
                                        'localhost', 0)
        client = httplib2.Http(proxy_info=proxy_info)
        self.assertRaises(httplib2.ProxiesUnavailableError,
                          client.request, 'http://localhost:-1/') 
Example #9
Source File: test_proxies.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def testSimpleProxy(self):
        proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP,
                                        'localhost', self.proxyport)
        client = httplib2.Http(proxy_info=proxy_info)
        src = 'miniserver.py'
        response, body = client.request('http://localhost:%d/%s' %
                                        (self.port, src))
        self.assertEqual(response.status, 200)
        self.assertEqual(body, open(os.path.join(miniserver.HERE, src)).read())
        lf = open(self.logfile).read()
        expect = ('Established connection to host "127.0.0.1" '
                  'using file descriptor')
        self.assertTrue(expect in lf,
                        'tinyproxy did not proxy a request for miniserver') 
Example #10
Source File: transport.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, timeout, proxy=None, cacert=None, sessions=False):
            ##httplib2.debuglevel=4
            kwargs = {}
            if proxy:
                import socks
                kwargs['proxy_info'] = httplib2.ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP, **proxy)
                print "using proxy", proxy

            # set optional parameters according supported httplib2 version
            if httplib2.__version__ >= '0.3.0':
                kwargs['timeout'] = timeout
            if httplib2.__version__ >= '0.7.0':
                kwargs['disable_ssl_certificate_validation'] = cacert is None
                kwargs['ca_certs'] = cacert    
            httplib2.Http.__init__(self, **kwargs) 
Example #11
Source File: authentication.py    From baobab with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, consumer_key, consumer_secret, token_key, token_secret):
        consumer = Consumer(key=consumer_key, secret=consumer_secret)
        token = Token(key=token_key, secret=token_secret)

        proxy_info = None
        if hasattr(settings, 'PROXY_HOST') and \
                hasattr(settings, 'PROXY_PORT'):
            proxy_info = ProxyInfo(
                proxy_type=PROXY_TYPE_HTTP,
                proxy_host=settings.PROXY_HOST,
                proxy_port=settings.PROXY_PORT)
        self.client = Client(
            consumer=consumer,
            token=token,
            proxy_info=proxy_info) 
Example #12
Source File: test_no_socket.py    From alfred-gmail with MIT License 5 votes vote down vote up
def testProxyDisabled(self):
        proxy_info = httplib2.ProxyInfo("blah", "localhost", 0)
        client = httplib2.Http(proxy_info=proxy_info)
        self.assertRaises(
            httplib2.ProxiesUnavailableError, client.request, "http://localhost:-1/"
        ) 
Example #13
Source File: gmail.py    From iris with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, config, proxy_cfg=None):
        self.config = config
        if proxy_cfg:
            proxy_info = ProxyInfo(
                socks.PROXY_TYPE_HTTP_NO_TUNNEL,
                proxy_cfg['host'],
                proxy_cfg['port']
            )
        else:
            proxy_info = None
        self.http = Http(proxy_info=proxy_info)
        self.client = None 
Example #14
Source File: test_no_socket.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def testProxyDisabled(self):
        proxy_info = httplib2.ProxyInfo('blah',
                                        'localhost', 0)
        client = httplib2.Http(proxy_info=proxy_info)
        self.assertRaises(httplib2.ProxiesUnavailableError,
                          client.request, 'http://localhost:-1/') 
Example #15
Source File: test_proxies.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def testSimpleProxy(self):
        proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP,
                                        'localhost', self.proxyport)
        client = httplib2.Http(proxy_info=proxy_info)
        src = 'miniserver.py'
        response, body = client.request('http://localhost:%d/%s' %
                                        (self.port, src))
        self.assertEqual(response.status, 200)
        self.assertEqual(body, open(os.path.join(miniserver.HERE, src)).read())
        lf = open(self.logfile).read()
        expect = ('Established connection to host "127.0.0.1" '
                  'using file descriptor')
        self.assertTrue(expect in lf,
                        'tinyproxy did not proxy a request for miniserver') 
Example #16
Source File: test_no_socket.py    From earthengine with MIT License 5 votes vote down vote up
def testProxyDisabled(self):
        proxy_info = httplib2.ProxyInfo('blah',
                                        'localhost', 0)
        client = httplib2.Http(proxy_info=proxy_info)
        self.assertRaises(httplib2.ProxiesUnavailableError,
                          client.request, 'http://localhost:-1/') 
Example #17
Source File: test_proxies.py    From earthengine with MIT License 5 votes vote down vote up
def testSimpleProxy(self):
        proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP,
                                        'localhost', self.proxyport)
        client = httplib2.Http(proxy_info=proxy_info)
        src = 'miniserver.py'
        response, body = client.request('http://localhost:%d/%s' %
                                        (self.port, src))
        self.assertEqual(response.status, 200)
        self.assertEqual(body, open(os.path.join(miniserver.HERE, src)).read())
        lf = open(self.logfile).read()
        expect = ('Established connection to host "127.0.0.1" '
                  'using file descriptor')
        self.assertTrue(expect in lf,
                        'tinyproxy did not proxy a request for miniserver') 
Example #18
Source File: http.py    From misp42splunk with GNU Lesser General Public License v3.0 4 votes vote down vote up
def get_proxy_info(proxy_config):
    if not proxy_config or not is_true(proxy_config.get('proxy_enabled')):
        _logger.info('Proxy is not enabled')
        return None

    url = proxy_config.get('proxy_url')
    port = proxy_config.get('proxy_port')

    if url or port:
        if not url:
            raise ValueError('Proxy "url" must not be empty')
        if not util.is_valid_port(port):
            raise ValueError(
                'Proxy "port" must be in range [1,65535]: %s' % port
            )

    user = proxy_config.get('proxy_username')
    password = proxy_config.get('proxy_password')

    if not all((user, password)):
        _logger.info('Proxy has no credentials found')
        user, password = None, None

    proxy_type = proxy_config.get('proxy_type')
    proxy_type = proxy_type.lower() if proxy_type else 'http'

    if proxy_type in _PROXY_TYPE_MAP:
        ptv = _PROXY_TYPE_MAP[proxy_type]
    elif proxy_type in list(_PROXY_TYPE_MAP.values()):
        ptv = proxy_type
    else:
        ptv = socks.PROXY_TYPE_HTTP
        _logger.info('Proxy type not found, set to "HTTP"')

    rdns = is_true(proxy_config.get('proxy_rdns'))

    proxy_info = ProxyInfo(
        proxy_host=url,
        proxy_port=int(port),
        proxy_type=ptv,
        proxy_user=user,
        proxy_pass=password,
        proxy_rdns=rdns
    )
    return proxy_info 
Example #19
Source File: http.py    From misp42splunk with GNU Lesser General Public License v3.0 4 votes vote down vote up
def get_proxy_info(proxy_config):
    if not proxy_config or not is_true(proxy_config.get('proxy_enabled')):
        _logger.info('Proxy is not enabled')
        return None

    url = proxy_config.get('proxy_url')
    port = proxy_config.get('proxy_port')

    if url or port:
        if not url:
            raise ValueError('Proxy "url" must not be empty')
        if not util.is_valid_port(port):
            raise ValueError(
                'Proxy "port" must be in range [1,65535]: %s' % port
            )

    user = proxy_config.get('proxy_username')
    password = proxy_config.get('proxy_password')

    if not all((user, password)):
        _logger.info('Proxy has no credentials found')
        user, password = None, None

    proxy_type = proxy_config.get('proxy_type')
    proxy_type = proxy_type.lower() if proxy_type else 'http'

    if proxy_type in _PROXY_TYPE_MAP:
        ptv = _PROXY_TYPE_MAP[proxy_type]
    elif proxy_type in list(_PROXY_TYPE_MAP.values()):
        ptv = proxy_type
    else:
        ptv = socks.PROXY_TYPE_HTTP
        _logger.info('Proxy type not found, set to "HTTP"')

    rdns = is_true(proxy_config.get('proxy_rdns'))

    proxy_info = ProxyInfo(
        proxy_host=url,
        proxy_port=int(port),
        proxy_type=ptv,
        proxy_user=user,
        proxy_pass=password,
        proxy_rdns=rdns
    )
    return proxy_info