Python requests.ConnectionError() Examples

The following are 60 code examples for showing how to use requests.ConnectionError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

You may also check out all available functions/classes of the module requests , or try the search function .

Example 1
Project: InsightAgent   Author: insightfinder   File: verifyInsightCredentials.py    License: Apache License 2.0 6 votes vote down vote up
def sendData():
    global projectName
    global userInsightfinder
    global licenseKey
    alldata["userName"] = userInsightfinder
    alldata["operation"] = "verify"
    alldata["licenseKey"] = licenseKey
    alldata["projectName"] = projectName
    json_data = json.dumps(alldata)
    #print json_data
    url = serverUrl + "/api/v1/agentdatahelper"
    print serverUrl
    try:
        response = requests.post(url, data = json.loads(json_data))
    except requests.ConnectionError, e:
        print "Connection failure : " + str(e)
        print "Verification with InsightFinder credentials Failed"
        sys.exit(1) 
Example 2
Project: InsightAgent   Author: insightfinder   File: verifyInsightCredentials.py    License: Apache License 2.0 6 votes vote down vote up
def sendData():
    global projectName
    global userInsightfinder
    global licenseKey
    alldata["userName"] = userInsightfinder
    alldata["operation"] = "verify"
    alldata["licenseKey"] = licenseKey
    alldata["projectName"] = projectName
    json_data = json.dumps(alldata)
    #print json_data
    url = serverUrl + "/api/v1/agentdatahelper"
    print serverUrl
    try:
        response = requests.post(url, data = json.loads(json_data))
    except requests.ConnectionError, e:
        print "Connection failure : " + str(e)
        print "Verification with InsightFinder credentials Failed"
        sys.exit(1) 
Example 3
Project: InsightAgent   Author: insightfinder   File: script_runner.py    License: Apache License 2.0 6 votes vote down vote up
def verifyUser(username, licenseKey, projectName):
    alldata = {}
    alldata["userName"] = username
    alldata["operation"] = "verify"
    alldata["licenseKey"] = licenseKey
    alldata["projectName"] = projectName
    toSendDataJSON = json.dumps(alldata)

    url = parameters['serverUrl'] + "/api/v1/agentdatahelper"

    try:
        response = requests.post(url, data=json.loads(toSendDataJSON))
    except requests.ConnectionError, e:
        logger.error("Connection failure : " + str(e))
        logger.error("Verification with InsightFinder credentials Failed")
        return False 
Example 4
Project: harmony-ops   Author: harmony-one   File: test.py    License: MIT License 6 votes vote down vote up
def is_after_epoch(n):
    url = args.hmy_endpoint_src
    payload = """{
        "jsonrpc": "2.0",
        "method": "hmy_latestHeader",
        "params": [  ],
        "id": 1
    }"""
    headers = {
        'Content-Type': 'application/json'
    }
    try:
        response = requests.request('POST', url, headers=headers, data=payload, allow_redirects=False, timeout=3)
        body = json.loads(response.content)
        return int(body["result"]["epoch"]) > n
    except (requests.ConnectionError, KeyError):
        return False 
Example 5
Project: clashroyale   Author: cgrok   File: client.py    License: MIT License 6 votes vote down vote up
def _request(self, url, refresh=False, **params):
        if self.using_cache and refresh is False:  # refresh=True forces a request instead of using cache
            cache = self._resolve_cache(url, **params)
            if cache is not None:
                return cache
        method = params.get('method', 'GET')
        json_data = params.get('json', {})
        timeout = params.pop('timeout', None) or self.timeout
        if self.is_async:  # return a coroutine
            return self._arequest(url, **params)
        try:
            with self.session.request(
                method, url, timeout=timeout, headers=self.headers, params=params, json=json_data
            ) as resp:
                return self._raise_for_status(resp, resp.text, method=method)
        except requests.Timeout:
            raise NotResponding
        except requests.ConnectionError:
            raise NetworkError 
Example 6
Project: clashroyale   Author: cgrok   File: client.py    License: MIT License 6 votes vote down vote up
def _request(self, url, refresh=False, **params):
        if self.using_cache and refresh is False:  # refresh=True forces a request instead of using cache
            cache = self._resolve_cache(url, **params)
            if cache is not None:
                return cache
        if self.ratelimit[1] == 0 and time() < self.ratelimit[2] / 1000:
            if not url.endswith('/auth/stats'):
                raise RatelimitErrorDetected(self.ratelimit[2] / 1000 - time())
        if self.is_async:  # return a coroutine
            return self._arequest(url, **params)
        timeout = params.pop('timeout', None) or self.timeout
        try:
            with self.session.get(url, timeout=timeout, headers=self.headers, params=params) as resp:
                return self._raise_for_status(resp, resp.text, method='GET')
        except requests.Timeout:
            raise NotResponding
        except requests.ConnectionError:
            raise NetworkError 
Example 7
Project: zun   Author: openstack   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def _make_request(self, path, cni_envs, expected_status=None):
        method = 'POST'

        host = CONF.cni_daemon.cni_daemon_host
        port = CONF.cni_daemon.cni_daemon_port
        url = 'http://%s:%s/%s' % (host, port, path)
        try:
            LOG.debug('Making request to CNI Daemon. %(method)s %(path)s\n'
                      '%(body)s',
                      {'method': method, 'path': url, 'body': cni_envs})
            resp = requests.post(url, json=cni_envs,
                                 headers={'Connection': 'close'})
        except requests.ConnectionError:
            LOG.exception('Looks like %s:%s cannot be reached. '
                          'Is zun-cni-daemon running?', (host, port))
            raise
        LOG.debug('CNI Daemon returned "%(status)d %(reason)s".',
                  {'status': resp.status_code, 'reason': resp.reason})
        if expected_status and resp.status_code != expected_status:
            LOG.error('CNI daemon returned error "%(status)d %(reason)s".',
                      {'status': resp.status_code, 'reason': resp.reason})
            raise exception.CNIError('Got invalid status code from CNI daemon')
        return resp 
Example 8
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_location_google_breaks(self):
        """User passed location arg but google api gave error"""
        caught_exceptions = [
            requests.ConnectionError, requests.HTTPError, requests.Timeout]
        with mock.patch('requests.get') as mock_get:
            for exception in caught_exceptions:
                mock_get.side_effect = exception
                with capture_sys_output():
                    with self.assertRaises(RipeAtlasToolsException):
                        cmd = Command()
                        cmd.init_args(["--location", "blaaaa"])
                        cmd.run()
            mock_get.side_effect = Exception()
            with self.assertRaises(Exception):
                cmd = Command()
                cmd.init_args(["--location", "blaaaa"])
                cmd.run() 
Example 9
Project: bot   Author: python-discord   File: doc.py    License: MIT License 6 votes vote down vote up
def convert(ctx: commands.Context, url: str) -> str:
        """Convert url to Intersphinx inventory URL."""
        try:
            intersphinx.fetch_inventory(SPHINX_MOCK_APP, '', url)
        except AttributeError:
            raise commands.BadArgument(f"Failed to fetch Intersphinx inventory from URL `{url}`.")
        except ConnectionError:
            if url.startswith('https'):
                raise commands.BadArgument(
                    f"Cannot establish a connection to `{url}`. Does it support HTTPS?"
                )
            raise commands.BadArgument(f"Cannot connect to host with URL `{url}`.")
        except ValueError:
            raise commands.BadArgument(
                f"Failed to read Intersphinx inventory from URL `{url}`. "
                "Are you sure that it's a valid inventory file?"
            )
        return url 
Example 10
Project: firmware_password_manager   Author: univ-of-utah-marriott-library-apple   File: Skeleton_Key.py    License: MIT License 6 votes vote down vote up
def verify_network(self):
        """
        Verifies network availability.

        Host: 8.8.8.8 (google-public-dns-a.google.com)
        OpenPort: 53/tcp
        Service: domain (DNS/TCP)
        """

        try:
            _ = requests.get("https://dns.google.com", timeout=3)
            # _ = requests.get("https://8.8.8.8", timeout=3)
            return True
        except requests.ConnectionError as exception_message:
            self.logger.error("%s: Unknown error. [%s]" % (inspect.stack()[0][3], exception_message))
        return False 
Example 11
Project: python-webpack   Author: markfinger   File: build_server.py    License: MIT License 6 votes vote down vote up
def is_running(self, debug=False):
        try:
            res = requests.post(self.url)
        except requests.ConnectionError:
            if debug:
                print('connection error')
            return False

        if debug:
            print('res', 'static_code', res.status_code, 'text', res.text)

        # Hacky, but it works for now
        return (
            res.status_code == 200 and
            'webpack-build' in res.text and
            'Config file not defined' in res.text
        ) 
Example 12
Project: Watson   Author: TailorDev   File: watson.py    License: MIT License 6 votes vote down vote up
def _get_remote_projects(self):
        # import when required in order to reduce watson response time (#312)
        import requests
        if not hasattr(self, '_remote_projects'):
            dest, headers = self._get_request_info('projects')

            try:
                response = requests.get(dest, headers=headers)
                assert response.status_code == 200

                self._remote_projects = response.json()
            except requests.ConnectionError:
                raise WatsonError("Unable to reach the server.")
            except AssertionError:
                raise WatsonError(
                    u"An error occurred with the remote "
                    "server: {}".format(response.json())
                )

        return self._remote_projects['projects'] 
Example 13
Project: kuryr-kubernetes   Author: openstack   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def _make_request(self, path, cni_envs, expected_status=None):
        method = 'POST'

        address = config.CONF.cni_daemon.bind_address
        url = 'http://%s/%s' % (address, path)
        try:
            LOG.debug('Making request to CNI Daemon. %(method)s %(path)s\n'
                      '%(body)s',
                      {'method': method, 'path': url, 'body': cni_envs})
            resp = requests.post(url, json=cni_envs,
                                 headers={'Connection': 'close'})
        except requests.ConnectionError:
            LOG.exception('Looks like %s cannot be reached. Is kuryr-daemon '
                          'running?', address)
            raise
        LOG.debug('CNI Daemon returned "%(status)d %(reason)s".',
                  {'status': resp.status_code, 'reason': resp.reason})
        if expected_status and resp.status_code != expected_status:
            LOG.error('CNI daemon returned error "%(status)d %(reason)s".',
                      {'status': resp.status_code, 'reason': resp.reason})
            raise k_exc.CNIError('Got invalid status code from CNI daemon.')
        return resp 
Example 14
Project: dipper   Author: monarch-initiative   File: DipperUtil.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_hgnc_id_from_symbol(gene_symbol):
        """
        Get HGNC curie from symbol using monarch and mygene services
        :param gene_symbol:
        :return:
        """
        monarch_url = 'https://solr.monarchinitiative.org/solr/search/select'
        params = DipperUtil._get_solr_weight_settings()
        params["q"] = "{0} \"{0}\"".format(gene_symbol)
        params["fq"] = ["taxon:\"NCBITaxon:9606\"", "category:\"gene\""]
        gene_id = None
        try:
            monarch_request = requests.get(monarch_url, params=params)
            response = monarch_request.json()
            count = response['response']['numFound']
            if count > 0:
                gene_id = response['response']['docs'][0]['id']
        except requests.ConnectionError:
            print("error fetching {0}".format(monarch_url))

        return gene_id 
Example 15
Project: ideascube   Author: ideascube   File: test_catalog.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_catalog_update_cache_no_fail_if_remote_unavailable(mocker):
    from ideascube.serveradmin.catalog import Catalog
    from requests import ConnectionError

    mocker.patch('ideascube.serveradmin.catalog.urlretrieve',
                 side_effect=ConnectionError)

    c = Catalog()
    assert c._available == {}
    assert c._installed == {}

    c.add_remote(
        'foo', 'Content from Foo', 'http://example.com/not_existing')
    c.update_cache()
    assert c._available == {}
    assert c._installed == {} 
Example 16
Project: rucio   Author: rucio   File: main.py    License: Apache License 2.0 6 votes vote down vote up
def GET(self):
        try:
            data = param_input()
            response = get(str(data.file_location), cert=config_get('webui', 'usercert'), verify=False)
            if not response.ok:
                response.raise_for_status()
            cont = response.content
            file_like_object = BytesIO(cont)
            tar = open(mode='r:gz', fileobj=file_like_object)
            jsonResponse = {}
            for member in tar.getmembers():
                jsonResponse[member.name] = member.size
            header('Content-Type', 'application/json')
            return dumps(jsonResponse)
        except ConnectionError as err:
            raise generate_http_error(503, str(type(err)), str(err))
        except TarError as err:
            raise generate_http_error(415, str(type(err)), str(err))
        except IOError as err:
            raise generate_http_error(422, str(type(err)), str(err))
        except Exception as err:
            raise generate_http_error(500, str(type(err)), str(err)) 
Example 17
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        input_classification = params.get("classification")
        offset = params.get("offset")
        limit = params.get("limit")

        params = dict()
        if input_classification is not None:
            params["classification"] = input_classification
        params["offset"] = offset
        params["limit"] = limit

        try:
            response = self.connection.CLIENT.get(self.__URL, params=params)
            applications = response.json()["items"]
            applications = komand.helper.clean(applications)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            raise error

        return {"applications": applications} 
Example 18
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        input_state = params.get("state")
        offset = params.get("offset")
        limit = params.get("limit")

        params = dict()
        if input_state is not None:
            params["state"] = input_state.upper()  # CloudLock input is case-sensitive
        params["offset"] = offset
        params["limit"] = limit

        try:
            response = self.connection.CLIENT.get(self.__URL, params=params)

            policies = response.json()
            policies = komand.helper.clean(policies)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            raise error

        return {"policies": policies} 
Example 19
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        name = params.get("name")
        q = params.get("q")
        offset = params.get("offset")
        limit = params.get("limit")

        params = dict()
        if name is not None:
            params["name"] = name
        if q is not None:
            params["q"] = q
        params["offset"] = offset
        params["limit"] = limit

        try:
            response = self.connection.CLIENT.get(self.__URL, params=params)
            entries = response.json()["items"]
            entries = komand.helper.clean(entries)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            raise error
        return {"entries": entries} 
Example 20
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/tasks".format(org_id=org_id,
                                                                                          inc_id=inc_id)

        self.logger.info("Retrieving tasks for incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.get(url)
            tasks = response.json()
            tasks = komand.helper.clean(tasks)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"tasks": tasks} 
Example 21
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        incident = params.get("incident")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents".format(org_id=org_id)

        incident = json.dumps(incident)

        self.logger.info("Creating incident for organization %s..." % org_id)
        try:
            response = self.connection.SESSION.post(url=url, data=incident)
            new_incident = response.json()
            new_incident = komand.helper.clean(new_incident)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"incident": new_incident} 
Example 22
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/history".format(org_id=org_id,
                                                                                            inc_id=inc_id)

        self.logger.info("Retrieving history for incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.get(url)
            history = response.json()
            history = komand.helper.clean(history)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"incident_history": history} 
Example 23
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")
        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}".format(org_id=org_id,
                                                                                    inc_id=inc_id)

        self.logger.info("Retrieving incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.get(url)
            incident = response.json()
            incident = komand.helper.clean(incident)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"incident": incident} 
Example 24
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}".format(org_id=org_id,
                                                                                    inc_id=inc_id)

        self.logger.info("Creating incident for organization %s..." % org_id)
        try:
            response = self.connection.SESSION.delete(url=url)
            status = response.json()
            status = komand.helper.clean(status)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"status": status} 
Example 25
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/artifacts".format(org_id=org_id,
                                                                                              inc_id=inc_id)

        self.logger.info("Retrieving artifacts for incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.get(url)
            artifacts = response.json()
            artifacts = komand.helper.clean(artifacts)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"artifacts": artifacts} 
Example 26
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")
        artifact_id = params.get("artifact_id")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/artifacts/{artifact_id}".format(org_id=org_id,
                                                                                                            inc_id=inc_id,
                                                                                                            artifact_id=artifact_id)

        self.logger.info("Retrieving artifact for incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.get(url)
            artifact = response.json()
            artifact = komand.helper.clean(artifact)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"artifact": artifact} 
Example 27
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")
        artifact = params.get("artifact")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/artifacts".format(org_id=org_id,
                                                                                              inc_id=inc_id)

        artifact = json.dumps(artifact)

        self.logger.info("Creating artifact on incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.post(url=url, data=artifact)
            new_artifact = response.json()[0]
            new_artifact = komand.helper.clean(new_artifact)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"artifact": new_artifact} 
Example 28
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")
        patch = params.get("patch")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}".format(org_id=org_id,
                                                                                    inc_id=inc_id)

        patch = json.dumps(patch)

        self.logger.info("Patching incident %s..." % inc_id)
        try:
            response = self.connection.SESSION.patch(url=url, data=patch)
            patch_status = response.json()
            patch_status = komand.helper.clean(patch_status)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"patch_status": patch_status} 
Example 29
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")
        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/tasks".format(org_id=org_id,
                                                                                          inc_id=inc_id)
        json_body = params.get("body")
        json_body = json.dumps(json_body)

        try:
            response = self.connection.SESSION.post(url=url, data=json_body)
            identifier = response.json()["id"]

            if response.status_code != 200:
                self.logger.error("Error occurred - check the JSON body and ensure the data is correct.")

        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error %d: %s" % (response.status_code, error))
            raise

        return {"identifier": identifier} 
Example 30
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 6 votes vote down vote up
def run(self, params={}):
        org_id = params.get("organization_id")
        inc_id = params.get("incident_id")
        artifact_id = params.get("artifact_id")
        artifact = params.get("artifact")

        url = self.connection.API_BASE + "/orgs/{org_id}/incidents/{inc_id}/artifacts/{artifact_id}".format(org_id=org_id,
                                                                                                            inc_id=inc_id,
                                                                                                            artifact_id=artifact_id)

        artifact = json.dumps(artifact)

        self.logger.info("Updating artifact %s..." % artifact_id)
        try:
            response = self.connection.SESSION.put(url=url, data=artifact)
            updated_artifact = response.json()
            updated_artifact = komand.helper.clean(updated_artifact)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            self.logger.error("Error: %s" % error)
            raise

        return {"artifact": updated_artifact} 
Example 31
Project: httpswatch   Author: benjaminp   File: check_https.py    License: MIT License 6 votes vote down vote up
def check_http_page(info):
    http_redirect = info.new_check()
    try:
        resp, tree = fetch_through_redirects("http://{}".format(info.domain))
        info.http_redirects_to_https = resp.url.startswith("https://")
        if info.http_redirects_to_https:
            http_redirect.succeed("HTTP site redirects to HTTPS.")
        else:
            http_redirect.fail("HTTP site doesn&rsquo;t redirect to HTTPS.")
    except requests.Timeout:
        http_redirect.fail("The HTTP site times out.")
        return
    except requests.ConnectionError:
        http_redirect.fail("Nothing is listening on port 80")
        return
    except Not200 as e:
        http_redirect.fail("The HTTP site returns an error status ({}) on request.".format(e.status))
        return 
Example 32
Project: anime-downloader   Author: vn-ki   File: selescrape.py    License: The Unlicense 6 votes vote down vote up
def status_select(driver, url, status='hide'):
    '''
    For now it doesnt do what its name suggests, 
    I have planned to add a status reporter of the http response code.
    This part of the code is not removed because it is part of its core.
    Treat it like it isnt here.
    '''
    try:
        if status == 'hide':
            driver.get(url)
        elif status == 'show':
            r = requests.head(url)
            if r.status_code == 503:
                raise RuntimeError("This website's sevice is unavailable or has cloudflare on.")
            driver.get(url)
            return r.status_code
        else:
            driver.get(url)
    except requests.ConnectionError:
        raise RuntimeError("Failed to establish a connection using the requests library.") 
Example 33
Project: lambda-chef-node-cleanup   Author: awslabs   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def request(self, method, path, headers={}, data=None):
        auth_headers = sign_request(key=self.key, http_method=method,
            path=self.parsed_url.path+path.split('?', 1)[0], body=data,
            host=self.parsed_url.netloc, timestamp=datetime.datetime.utcnow(),
            user_id=self.client)
        request_headers = {}
        request_headers.update(self.headers)
        request_headers.update(dict((k.lower(), v) for k, v in six.iteritems(headers)))
        request_headers['x-chef-version'] = self.version
        request_headers.update(auth_headers)
        try:
            response = self._request(method, self.url + path, data, dict(
                (k.capitalize(), v) for k, v in six.iteritems(request_headers)))
        except requests.ConnectionError as e:
            raise ChefServerError(e.message)

        if not response.ok:
            raise ChefServerError.from_error(response.reason, code=response.status_code)

        return response 
Example 34
Project: appr   Author: app-registry   File: discovery.py    License: Apache License 2.0 6 votes vote down vote up
def discover_sources(package, version, media_type, secure=False):
    schemes = ["https://", "http://"]
    host, name = split_package_name(package)
    for scheme in schemes:
        url = scheme + host
        try:
            r = requests.get(url, params={"appr-discovery": 1}, timeout=2)
        except (requests.exceptions.Timeout, requests.ConnectionError) as e:
            if scheme == "https://" and not secure:
                continue
            else:
                raise e

        r.raise_for_status()
        variables = {
            'name': name,
            'version': version,
            "media_type": media_type,
            "mediatype": media_type}
        p = MetaHTMLParser(variables)
        p.feed(r.content.decode())
        if package in p.meta:
            return p.meta[package]
    return None 
Example 35
Project: trainline-python   Author: tducret   File: __init__.py    License: MIT License 6 votes vote down vote up
def _post(self, url, post_data, expected_status_code=200):
        trials = 0
        while trials <= _MAX_SERVER_RETRY:
            trials += 1
            ret = self.session.post(url=url,
                                    headers=self.headers,
                                    data=post_data)
            if (ret.status_code == expected_status_code):
                break
            else:
                time.sleep(_TIME_AFTER_FAILED_REQUEST)

        if (ret.status_code != expected_status_code):
            raise ConnectionError(
                'Status code {status} for url {url}\n{content}'.format(
                    status=ret.status_code, url=url, content=ret.text))
        return ret 
Example 36
Project: jumpserver-python-sdk   Author: jumpserver   File: request.py    License: GNU General Public License v2.0 5 votes vote down vote up
def do(self, api_name=None, pk=None, method='get', use_auth=True,
           data=None, params=None, content_type='application/json', **kwargs):

        if api_name in API_URL_MAPPING:
            path = API_URL_MAPPING.get(api_name)
            if pk and '%s' in path:
                path = path % pk
        else:
            path = api_name

        request_headers = kwargs.get('headers', {})
        default_headers = self.default_headers or {}
        headers = {k: v for k, v in default_headers.items()}
        headers.update(request_headers)
        kwargs['headers'] = headers
        url = self.endpoint.rstrip('/') + path
        req = HttpRequest(url, method=method, data=data,
                          params=params, content_type=content_type,
                          **kwargs)
        if use_auth:
            if not self.auth:
                msg = 'Authentication required, but not provide'
                logger.error(msg)
                raise RequestError(msg)
            else:
                self.auth.sign_request(req)

        try:
            resp = req.do()
        except (requests.ConnectionError, requests.ConnectTimeout) as e:
            msg = "Connect endpoint {} error: {}".format(self.endpoint, e)
            logger.error(msg)
            raise RequestError(msg)

        return self.clean_result(resp) 
Example 37
Project: News-At-Command-Line   Author: Griffintaur   File: NewsPulling.py    License: MIT License 5 votes vote down vote up
def PullNews(self):
        Configuration = ConfigurationReader()
        self.__APIKey=Configuration.GetAPIKEY()
        self.__Limit=Configuration.GetLimit()
        url='https://newsapi.org/v2/top-headlines?sources='+self.Source+'&sortBy=top&apiKey='+self.__APIKey
        try:
            req=requests.get(url)
            if(req.status_code==200):
                return req
            else:
                print "There is some issue in connecting to the internet. Please check your firewall or internet"
        except ConnectionError as e:
            print "A connection Attempt failed"
            print e.message
            sys.exit() 
Example 38
Project: sawtooth-cookiejar   Author: danintel   File: cookiejar_client.py    License: Apache License 2.0 5 votes vote down vote up
def _send_to_rest_api(self, suffix, data=None, content_type=None):
        '''Send a REST command to the Validator via the REST API.

           Called by count() &  _wrap_and_send().
           The latter caller is made on the behalf of bake() & eat().
        '''
        url = "{}/{}".format(self._base_url, suffix)
        print("URL to send to REST API is {}".format(url))

        headers = {}

        if content_type is not None:
            headers['Content-Type'] = content_type

        try:
            if data is not None:
                result = requests.post(url, headers=headers, data=data)
            else:
                result = requests.get(url, headers=headers)

            if not result.ok:
                raise Exception("Error {}: {}".format(
                    result.status_code, result.reason))
        except requests.ConnectionError as err:
            raise Exception(
                'Failed to connect to {}: {}'.format(url, str(err)))
        except BaseException as err:
            raise Exception(err)

        return result.text 
Example 39
Project: InsightAgent   Author: insightfinder   File: reportMetrics.py    License: Apache License 2.0 5 votes vote down vote up
def get_ec2_instance_type():
    url = "http://169.254.169.254/latest/meta-data/instance-type" # TODO: fix/remove
    try:
        response = requests.post(url)
    except requests.ConnectionError, e:
        logger.error("Error finding instance-type")
        return 
Example 40
Project: InsightAgent   Author: insightfinder   File: proxy_reportMetrics.py    License: Apache License 2.0 5 votes vote down vote up
def ec2InstanceType():
    url = "http://169.254.169.254/latest/meta-data/instance-type"
    try:
        response = requests.post(url)
    except requests.ConnectionError, e:
        print "Error finding instance-type"
        return 
Example 41
Project: controller   Author: deis   File: test_kubehttpclient.py    License: MIT License 5 votes vote down vote up
def connection_refused_matcher(request):
    raise requests.ConnectionError("connection refused") 
Example 42
Project: facebook-wda   Author: openatx   File: __init__.py    License: MIT License 5 votes vote down vote up
def fetch(self, method, url, data=None):
        """
        Raises:
            WDAError
        """
        try:
            return self._fetch_with_autofix(method, url, data)
        except WDARequestError as e:
            raise WDAError("Request failed: http {} {}".format(method.upper(), url), str(e))
        except requests.ConnectionError as e:
            raise WDAError("Failed to establish connection to to WDA") 
Example 43
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    License: GNU General Public License v3.0 5 votes vote down vote up
def location2degrees(self):
        """Fetches degrees based on the given location."""
        error_log = (
            "Following error occured while trying to fetch lat/lon"
            "for location <{}>:\n{}"
        )
        goole_api_url = "http://maps.googleapis.com/maps/api/geocode/json"
        try:
            result = requests.get(goole_api_url, params={
                "sensor": "false",
                "address": self.arguments.location
            })
        except (
            requests.ConnectionError,
            requests.HTTPError,
            requests.Timeout,
        ) as e:
            error_log = error_log.format(self.arguments.location, e)
            raise RipeAtlasToolsException(error_log)

        result = result.json()

        try:
            lat = result["results"][0]["geometry"]["location"]["lat"]
            lng = result["results"][0]["geometry"]["location"]["lng"]
        except (KeyError, IndexError) as e:
            error = error_log.format(self.arguments.location, e)
            raise RipeAtlasToolsException(error)

        return str(lat), str(lng) 
Example 44
Project: bot   Author: python-discord   File: doc.py    License: MIT License 5 votes vote down vote up
def _fetch_inventory(self, inventory_url: str) -> Optional[dict]:
        """Get and return inventory from `inventory_url`. If fetching fails, return None."""
        fetch_func = functools.partial(intersphinx.fetch_inventory, SPHINX_MOCK_APP, '', inventory_url)
        for retry in range(1, FAILED_REQUEST_RETRY_AMOUNT+1):
            try:
                package = await self.bot.loop.run_in_executor(None, fetch_func)
            except ConnectTimeout:
                log.error(
                    f"Fetching of inventory {inventory_url} timed out,"
                    f" trying again. ({retry}/{FAILED_REQUEST_RETRY_AMOUNT})"
                )
            except ProtocolError:
                log.error(
                    f"Connection lost while fetching inventory {inventory_url},"
                    f" trying again. ({retry}/{FAILED_REQUEST_RETRY_AMOUNT})"
                )
            except HTTPError as e:
                log.error(f"Fetching of inventory {inventory_url} failed with status code {e.response.status_code}.")
                return None
            except ConnectionError:
                log.error(f"Couldn't establish connection to inventory {inventory_url}.")
                return None
            else:
                return package
        log.error(f"Fetching of inventory {inventory_url} failed.")
        return None 
Example 45
Project: firmware_password_manager   Author: univ-of-utah-marriott-library-apple   File: firmware_password_manager.py    License: MIT License 5 votes vote down vote up
def verify_network(self):
        """
        Host: 8.8.8.8 (google-public-dns-a.google.com)
        OpenPort: 53/tcp
        Service: domain (DNS/TCP)
        """

        try:
            _ = requests.get("https://8.8.8.8", timeout=3)
            return True
        except requests.ConnectionError as exception_message:
            print(exception_message)
        return False 
Example 46
Project: python-ssllabs   Author: takeshixx   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def _check_api_info(self):
        try:
            if not self.API_URL:
                for url in self.API_URLS:
                    try:
                        response = self._handle_api_error(requests.get('{}info'.format(url))).json()
                        self.API_URL = url
                        break
                    except requests.ConnectionError:
                        continue
            else:
                try:
                    response = self._handle_api_error(requests.get('{}info'.format(self.API_URL))).json()
                except requests.ConnectionError:
                    self._die_on_error('[ERROR] Provided API URL is unavailable.')

            if not self.API_URL:
                self._die_on_error('[ERROR] SSL Labs APIs are down. Please try again later.')

            self.CLIENT_MAX_ASSESSMENTS = response.get('clientMaxAssessments')
            self.CURRENT_ASSESSMENTS = response.get('currentAssessments')
            self.MAX_ASSESSMENTS = response.get('maxAssessments')
            if self.MAX_ASSESSMENTS<=0:
                LOGGER.debug('Rate limit reached')
                return False
            LOGGER.info('[NOTICE] SSL Labs v{engine_version} (criteria version '
                        '{criteria_version})'.format(
                            engine_version=response.get('engineVersion'),
                            criteria_version=response.get('criteriaVersion')))
            LOGGER.info('[NOTICE] {server_message}'.format(
                server_message=response.get('messages')[0]))
            return True
        except AccessProblem as e:
            raise
        except Exception as e:
            LOGGER.exception(e)
            return False 
Example 47
Project: Watson   Author: TailorDev   File: watson.py    License: MIT License 5 votes vote down vote up
def pull(self):
        import requests
        dest, headers = self._get_request_info('frames')

        try:
            response = requests.get(
                dest, params={'last_sync': self.last_sync}, headers=headers
            )
            assert response.status_code == 200
        except requests.ConnectionError:
            raise WatsonError("Unable to reach the server.")
        except AssertionError:
            raise WatsonError(
                u"An error occurred with the remote "
                "server: {}".format(response.json())
            )

        frames = response.json() or ()

        for frame in frames:
            frame_id = uuid.UUID(frame['id']).hex
            self.frames[frame_id] = (
                frame['project'],
                frame['begin_at'],
                frame['end_at'],
                frame['tags']
            )

        return frames 
Example 48
Project: Watson   Author: TailorDev   File: watson.py    License: MIT License 5 votes vote down vote up
def push(self, last_pull):
        import requests
        dest, headers = self._get_request_info('frames/bulk')

        frames = []

        for frame in self.frames:
            if last_pull > frame.updated_at > self.last_sync:
                frames.append({
                    'id': uuid.UUID(frame.id).urn,
                    'begin_at': str(frame.start.to('utc')),
                    'end_at': str(frame.stop.to('utc')),
                    'project': frame.project,
                    'tags': frame.tags
                })

        try:
            response = requests.post(dest, json.dumps(frames), headers=headers)
            assert response.status_code == 201
        except requests.ConnectionError:
            raise WatsonError("Unable to reach the server.")
        except AssertionError:
            raise WatsonError(
                u"An error occurred with the remote server (status: {}). "
                u"Response was:\n{}".format(
                    response.status_code,
                    response.text
                )
            )

        return frames 
Example 49
Project: kuryr-kubernetes   Author: openstack   File: test_api.py    License: Apache License 2.0 5 votes vote down vote up
def test_run_socket_error(self, m_post):
        m_post.side_effect = requests.ConnectionError
        result = self._test_run('DEL', 'delNetwork', m_post)
        self.assertEqual(1, result) 
Example 50
Project: ideascube   Author: ideascube   File: catalog.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def update_cache(self):
        self._available_value = {}

        for remote in self._remotes.values():
            # TODO: Get resumable.urlretrieve to accept a file-like object?
            with tempfile.NamedTemporaryFile() as fd:
                tmppath = fd.name

                def _progress(*args):
                    self._progress(remote.name, *args)

                # TODO: Verify the download with sha256sum? Crypto signature?
                try:
                    urlretrieve(remote.url, tmppath, reporthook=_progress)

                except ConnectionError:
                    print("Warning: Impossible to connect to the remote "
                          "{remote.name}({remote.url}).\n"
                          "Continuing without it.".format(remote=remote))
                    continue

                catalog = load_from_yml_file(tmppath)

                # TODO: Handle content which was removed from the remote source
                self._available.update(catalog['all'])

        self._update_installed_metadata()
        self._persist_catalog() 
Example 51
Project: im   Author: grycap   File: probeim.py    License: GNU General Public License v3.0 5 votes vote down vote up
def requestIM(method, url, data, headers, verify):
        try:
            logging.debug(method, url, data, headers)
            r = requests.request(method, url, data=data, headers=headers, verify=verify)
            rq = ResponseIM(r.status_code, r.text)
        except requests.ConnectionError as e:
            logging.error("* ConnectionError exception at method " + method + ": " + str(e))
            rq = ResponseIM(111, e)
        except requests.exceptions.InvalidHeader as e:
            logging.error("* InvalidHeader exception: ---> " + str(e) + " <--")
            rq = ResponseIM(111, e)

        return rq 
Example 52
Project: rucio   Author: rucio   File: main.py    License: Apache License 2.0 5 votes vote down vote up
def GET(self):
        try:
            pyDict = {}
            data = param_input()
            response = get(str(data.file_location), cert=config_get('webui', 'usercert'), verify=False)
            if not response.ok:
                response.raise_for_status()
            cont = response.content
            file_like_object = BytesIO(cont)
            tar = open(mode='r:gz', fileobj=file_like_object)
            for member in tar.getmembers():
                if member.name == str(data.file_name):
                    try:
                        f = tar.extractfile(member)
                        pyDict['content'] = f.read(16000000)
                        pyDict['size'] = f.tell()
                        jsonResponse = dumps(pyDict)
                        tar.close()
                        return jsonResponse
                    except UnicodeDecodeError:
                        f = tar.extractfile(member)
                        out = GzipFile(fileobj=f)
                        pyDict['content'] = out.read(16000000)
                        pyDict['size'] = out.tell()
                        jsonResponse = dumps(pyDict)
                        tar.close()
                        return jsonResponse
                    return "ok"
        except ConnectionError as err:
            raise generate_http_error(503, str(type(err)), str(err))
        except TarError as err:
            raise generate_http_error(415, str(type(err)), str(err))
        except IOError as err:
            raise generate_http_error(422, str(type(err)), str(err))
        except Exception as err:
            raise generate_http_error(500, str(type(err)), str(err)) 
Example 53
Project: insightconnect-plugins   Author: rapid7   File: sitereview.py    License: MIT License 5 votes vote down vote up
def site_review(self, url):
        payload = {"url": url}

        try:
            self.req = requests.post(self.baseurl, headers=self.headers, json=payload)
        except requests.ConnectionError:
            raise Exception("[-] ConnectionError: A connection error occurred")

        return xmltodict.parse(self.req.text) 
Example 54
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def test(self):
        url = "https://api.cloudlock.com/api/v2/activities"
        try:
            response = self.connection.CLIENT.get(url)
        except (requests.ConnectionError, requests.HTTPError) as error:
            raise error
        return {} 
Example 55
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def test(self):
        url = "https://api.cloudlock.com/api/v2/activities"
        try:
            response = self.connection.CLIENT.get(url)
        except (requests.ConnectionError, requests.HTTPError) as error:
            raise error
        return {} 
Example 56
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def test(self):
        url = "https://api.cloudlock.com/api/v2/activities"
        try:
            response = self.connection.CLIENT.get(url)
        except (requests.ConnectionError, requests.HTTPError) as error:
            raise error
        return {} 
Example 57
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def test(self):
        url = "https://api.cloudlock.com/api/v2/activities"
        try:
            response = self.connection.CLIENT.get(url)
        except (requests.ConnectionError, requests.HTTPError) as error:
            raise error
        return {} 
Example 58
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def run(self, params={}):
        try:
            response = self.connection.CLIENT.get(self.__URL, params=params)
            activities = response.json()["items"]
            activities = komand.helper.clean(activities)
        except (requests.ConnectionError, requests.HTTPError, KeyError, ValueError) as error:
            raise error
        return {"activities": activities} 
Example 59
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def test(self):
        url = "https://api.cloudlock.com/api/v2/activities"
        try:
            response = self.connection.CLIENT.get(url)
        except (requests.ConnectionError, requests.HTTPError) as error:
            raise error
        return {} 
Example 60
Project: insightconnect-plugins   Author: rapid7   File: action.py    License: MIT License 5 votes vote down vote up
def test(self):
        url = "https://api.cloudlock.com/api/v2/activities"
        try:
            response = self.connection.CLIENT.get(url)
        except (requests.ConnectionError, requests.HTTPError) as error:
            raise error
        return {}