Python requests.exceptions.InvalidURL() Examples

The following are 25 code examples of requests.exceptions.InvalidURL(). 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 requests.exceptions , or try the search function .
Example #1
Source File: test_connector.py    From infoblox-client with Apache License 2.0 7 votes vote down vote up
def test_neutron_exception_is_raised_on_any_request_error(self):
        # timeout exception raises InfobloxTimeoutError
        f = mock.Mock()
        f.__name__ = 'mock'
        f.side_effect = req_exc.Timeout
        self.assertRaises(exceptions.InfobloxTimeoutError,
                          connector.reraise_neutron_exception(f))

        # all other request exception raises InfobloxConnectionError
        supported_exceptions = [req_exc.HTTPError,
                                req_exc.ConnectionError,
                                req_exc.ProxyError,
                                req_exc.SSLError,
                                req_exc.TooManyRedirects,
                                req_exc.InvalidURL]

        for ex in supported_exceptions:
            f.side_effect = ex
            self.assertRaises(exceptions.InfobloxConnectionError,
                              connector.reraise_neutron_exception(f)) 
Example #2
Source File: test_http.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_hook_with_method_in_lowercase(self, mock_requests, request_mock):
        from requests.exceptions import MissingSchema, InvalidURL
        with mock.patch(
            'airflow.hooks.base_hook.BaseHook.get_connection',
            side_effect=get_airflow_connection_with_port
        ):
            data = "test params"
            try:
                self.get_lowercase_hook.run('v1/test', data=data)
            except (MissingSchema, InvalidURL):
                pass
            request_mock.assert_called_once_with(
                mock.ANY,
                mock.ANY,
                headers=mock.ANY,
                params=data
            ) 
Example #3
Source File: helpers.py    From micropy-cli with MIT License 6 votes vote down vote up
def ensure_valid_url(url):
    """Ensure a url is valid.

    Args:
        url (str): URL to validate

    Raises:
        InvalidURL: URL is not a valid url
        ConnectionError: Failed to connect to url
        HTTPError: Reponse was not 200 <OK>

    Returns:
        str: valid url

    """
    if not is_url(url):
        raise reqexc.InvalidURL(f"{url} is not a valid url!")
    resp = requests.head(url, allow_redirects=True)
    resp.raise_for_status()
    return url 
Example #4
Source File: connector.py    From indico-plugins with MIT License 6 votes vote down vote up
def _validate_server_url(self):
        """Validates self.server_url"""
        try:
            request = requests.head(self.server_url)
            if request.status_code >= 400:
                raise InvenioConnectorServerError(
                    "Unexpected status code '%d' accessing URL: %s"
                    % (request.status_code, self.server_url))
        except (InvalidSchema, MissingSchema) as err:
            raise InvenioConnectorServerError(
                "Bad schema, expecting http:// or https://:\n %s" % (err,))
        except ConnectionError as err:
            raise InvenioConnectorServerError(
                "Couldn't establish connection to '%s':\n %s"
                % (self.server_url, err))
        except InvalidURL as err:
            raise InvenioConnectorServerError(
                "Invalid URL '%s':\n %s"
                % (self.server_url, err))
        except RequestException as err:
            raise InvenioConnectorServerError(
                "Unknown error connecting to '%s':\n %s"
                % (self.server_url, err)) 
Example #5
Source File: project.py    From rasa_nlu with Apache License 2.0 6 votes vote down vote up
def _update_model_from_server(model_server: EndpointConfig,
                              project: 'Project') -> None:
    """Load a zipped Rasa NLU model from a URL and update the passed

    project."""
    if not is_url(model_server.url):
        raise InvalidURL(model_server)

    model_directory = tempfile.mkdtemp()

    new_model_fingerprint, filename = _pull_model_and_fingerprint(
        model_server, model_directory, project.fingerprint)
    if new_model_fingerprint:
        model_name = _get_remote_model_name(filename)
        project.fingerprint = new_model_fingerprint
        project.update_model_from_dir_and_unload_others(model_directory,
                                                        model_name)
    else:
        logger.debug("No new model found at URL {}".format(model_server.url)) 
Example #6
Source File: PackageRequest.py    From FXTest with MIT License 5 votes vote down vote up
def post(self, url, params,headers):#post消息
        data = json.dumps(params)
        try:
            self.r =requests.post(url,params=data,headers=headers,timeout=Interface_Time_Out)
            json_response = json.loads(self.r.text)
            spend=self.r.elapsed.total_seconds()
            return json_response,spend
        except exceptions.Timeout :
            return {'post请求出错': "请求超时" }
        except exceptions.InvalidURL:
            return {'post请求出错': "非法url"}
        except exceptions.HTTPError:
            return {'post请求出错': "http请求错误"}
        except Exception as e:
            return {'post请求出错': "错误原因:%s" % e} 
Example #7
Source File: http.py    From esdc-ce with Apache License 2.0 5 votes vote down vote up
def __init__(self, url, validate_url=True):
        self.url = url

        if validate_url and not self.is_valid():
            raise InvalidURL('Invalid URL') 
Example #8
Source File: exceptions.py    From labkey-api-python with Apache License 2.0 5 votes vote down vote up
def _get_message(server_context, e):
        switcher = {
            exceptions.ConnectionError:
                'Failed to connect to server. Ensure the server_context domain, context_path, '
                'and SSL are configured correctly.',
            exceptions.InvalidURL:
                'Failed to parse URL. Context is ' + str(server_context),
            exceptions.SSLError:
                'Failed to match server SSL configuration. Ensure the server_context is configured correctly.'
        }
        # #12 Pass through the exception message if available
        return switcher.get(type(e), str(e) if str(e) else 'Please verify server_context is configured correctly.') 
Example #9
Source File: requests_tests.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_requests_instrumentation_malformed_path(instrument, elasticapm_client):
    elasticapm_client.begin_transaction("transaction.test")
    with capture_span("test_request", "test"):
        with pytest.raises(InvalidURL):
            requests.get("http://") 
Example #10
Source File: __init__.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode:
    if isinstance(
        exc,
        (InvalidURL, InvalidSchema, MissingSchema, URLRequired, ValueError),
    ):
        return StatusCanonicalCode.INVALID_ARGUMENT
    if isinstance(exc, Timeout):
        return StatusCanonicalCode.DEADLINE_EXCEEDED
    return StatusCanonicalCode.UNKNOWN 
Example #11
Source File: test_http_checks.py    From syntribos with Apache License 2.0 5 votes vote down vote up
def test_invalid_url(self):
        signal = http_checks.check_fail(rex.InvalidURL())
        self._assert_has_tags(self.bad_request_tags, signal)
        self._assert_has_slug("HTTP_FAIL_INVALID_URL", signal) 
Example #12
Source File: vespa.py    From integrations-extras with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check(self, instance):
        self.metric_count = 0
        self.services_up = 0

        instance_tags = instance.get('tags', [])
        consumer = instance.get('consumer')
        if not consumer:
            raise CheckException("The consumer must be specified in the configuration.")
        url = self.URL + '?consumer=' + consumer
        try:
            json = self._get_metrics_json(url)
            if 'services' not in json:
                self.service_check(self.METRICS_SERVICE_CHECK, AgentCheck.WARNING, tags=instance_tags,
                                   message="No services in response from metrics proxy on {}".format(url))
                return

            for service in json['services']:
                service_name = service['name']
                self._report_service_status(instance_tags, service_name, service)
                for metrics in service['metrics']:
                    self._emit_metrics(service_name, metrics, instance_tags)

            self.log.info("Forwarded %s metrics to hq for %s services", self.metric_count, self.services_up)
            self.service_check(self.METRICS_SERVICE_CHECK, AgentCheck.OK, tags=instance_tags,
                               message="Metrics collected successfully for consumer {}".format(consumer))
        except Timeout as e:
            self._report_metrics_error("Timed out connecting to Vespa's node metrics api: {}".format(e),
                                       AgentCheck.CRITICAL, instance_tags)
        except (HTTPError, InvalidURL, ConnectionError) as e:
            self._report_metrics_error("Could not connect to Vespa's node metrics api: {}".format(e),
                                       AgentCheck.CRITICAL, instance_tags)
        except JSONDecodeError as e:
            self._report_metrics_error("Error parsing JSON from Vespa's node metrics api: {}".format(e),
                                       AgentCheck.CRITICAL, instance_tags)
        except Exception as e:
            self._report_metrics_error("Unexpected error: {}".format(e),
                                       AgentCheck.WARNING, instance_tags) 
Example #13
Source File: test_connect_command.py    From cauldron with MIT License 5 votes vote down vote up
def test_invalid_url_error(self, requests_get: MagicMock):
        """Should fail if the url is not valid."""

        requests_get.side_effect = request_exceptions.InvalidURL('Fake')

        r = support.run_command('connect "a url"')
        self.assert_has_error_code(r, 'INVALID_URL') 
Example #14
Source File: test_utils.py    From micropy-cli with MIT License 5 votes vote down vote up
def test_ensure_valid_url(mocker, test_urls):
    """should ensure url is valid"""
    u = test_urls
    with pytest.raises(InvalidURL):
        utils.ensure_valid_url(test_urls['invalid'])
    with pytest.raises(ConnectionError):
        mocker.patch.object(utils, "is_url", return_value=True)
        mock_head = mocker.patch.object(requests, "head")
        mock_head.side_effect = [ConnectionError]
        utils.ensure_valid_url(u['valid'])
    mocker.stopall()
    with pytest.raises(HTTPError):
        utils.ensure_valid_url(u['bad_resp'])
    result = utils.ensure_valid_url(u['valid'])
    assert result == u['valid'] 
Example #15
Source File: utils.py    From rasa_core with Apache License 2.0 5 votes vote down vote up
def download_file_from_url(url: Text) -> Text:
    """Download a story file from a url and persists it into a temp file.

    Returns the file path of the temp file that contains the
    downloaded content."""
    from rasa_nlu import utils as nlu_utils

    if not nlu_utils.is_url(url):
        raise InvalidURL(url)

    async with aiohttp.ClientSession() as session:
        async with session.get(url, raise_for_status=True) as resp:
            filename = nlu_utils.create_temporary_file(await resp.read(),
                                                       mode="w+b")

    return filename 
Example #16
Source File: PackageRequest.py    From FXTest with MIT License 5 votes vote down vote up
def putfile(self,url,params,headers):#put请求
        try:
            self.rdata=json.dumps(params)
            me=requests.put(url,self.rdata,headers=headers,timeout=Interface_Time_Out)
            json_response=json.loads(me.text)
            spend=me.elapsed.total_seconds()
            return json_response,spend
        except exceptions.Timeout :
            return {'put请求出错': "请求超时" }
        except exceptions.InvalidURL:
            return {'put请求出错': "非法url"}
        except exceptions.HTTPError:
            return {'put请求出错': "http请求错误"}
        except Exception as e:
            return {'put请求出错': "错误原因:%s" % e} 
Example #17
Source File: PackageRequest.py    From FXTest with MIT License 5 votes vote down vote up
def delfile(self,url,params,headers):#删除的请求
        try:
            self.rdel_word=requests.delete(url,data=params,headers=headers,timeout=Interface_Time_Out)
            json_response=json.loads(self.rdel_word.text)
            spend=self.rdel_word.elapsed.total_seconds()
            return json_response,spend
        except exceptions.Timeout :
            return {'delete请求出错': "请求超时" }
        except exceptions.InvalidURL:
            return {'delete请求出错': "非法url"}
        except exceptions.HTTPError:
            return {'delete请求出错': "http请求错误"}
        except Exception as e:
            return {'delete请求出错': "错误原因:%s" % e} 
Example #18
Source File: check.py    From integrations-extras with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _get_response_from_url(self, url, timeout, aggregation_key, instance_tags):
        """
        Send rest request to address and return the response as JSON
        """
        response = None
        self.log.debug('Sending request to "%s"', url)

        try:
            response = requests.get(url, timeout=timeout)
            response.raise_for_status()
            response = response.json()

        except Timeout as e:
            self.service_check(
                self.SORTDB_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=instance_tags,
                message="Request timeout: {}, {}".format(url, e),
            )
            self.timeout_event(url, timeout, aggregation_key)
            raise

        except (HTTPError, InvalidURL, ConnectionError) as e:
            self.service_check(
                self.SORTDB_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=instance_tags,
                message="Request failed: {0}, {1}".format(url, e),
            )
            raise

        except JSONDecodeError as e:
            self.service_check(
                self.SORTDB_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=instance_tags,
                message='JSON Parse failed: {0}, {1}'.format(url, e),
            )
            raise

        except ValueError as e:
            self.service_check(self.SORTDB_SERVICE_CHECK, AgentCheck.CRITICAL, tags=instance_tags, message=str(e))
            raise

        return response 
Example #19
Source File: connect.py    From cauldron with MIT License 4 votes vote down vote up
def check_connection(url: str, force: bool) -> Response:
    """..."""
    response = Response()
    if force:
        return response

    ping = '{}/'.format(url)

    response.notify(
        kind='STARTING',
        code='CONNECTING',
        message='Establishing remote kernel connection to: {}'.format(url)
    ).console(
        whitespace_top=1
    )

    try:
        result = requests.get(ping)

        if result.status_code != 200:
            raise request_exceptions.ConnectionError()

        return response.notify(
            kind='CONNECTED',
            code='REMOTE_CONNECTION_ESTABLISHED',
            message='Remote connection established.'
        ).console(whitespace=1).response
    except request_exceptions.InvalidURL as error:
        return response.fail(
            code='INVALID_URL',
            message='Invalid connection URL. Unable to establish connection',
            error=error
        ).console(
            whitespace=1
        ).response
    except request_exceptions.ConnectionError as error:
        return response.fail(
            code='CONNECTION_ERROR',
            message='Unable to connect to remote cauldron host',
            error=error
        ).console(
            whitespace=1
        ).response
    except Exception as error:
        return response.fail(
            code='CONNECT_COMMAND_ERROR',
            message='Failed to connect to the remote cauldron host',
            error=error
        ).console(
            whitespace=1
        ).response 
Example #20
Source File: http.py    From syntribos with Apache License 2.0 4 votes vote down vote up
def check_fail(exception):
    """Checks for a requestslib exception, returns a signal if found.

    If this Exception is an instance of
    :class:`requests.exceptions.RequestException`, determine what kind of
    exception was raised. If not, return the results of from_generic_exception.

    :param Exception exception: An Exception object
    :returns: Signal with exception details
    :rtype: :class:`syntribos.signal.SynSignal`
    """
    check_name = "HTTP_CHECK_FAIL"

    def uncamel(string):
        string = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", string)
        return re.sub("([a-z0-9])([A-Z])", r"\1_\2", string).upper()

    if not isinstance(exception, rex.RequestException):
        return syntribos.signal.from_generic_exception(exception)

    data = {
        "response": exception.response,
        "request": exception.request,
        "exception": exception,
        "exception_name": uncamel(exception.__class__.__name__)
    }
    text = "An exception was encountered when sending the request. {desc}"
    slug = "HTTP_FAIL_{exc}".format(exc=data["exception_name"])
    tags = set(["EXCEPTION_RAISED"])

    invalid_request_exceptions = (rex.URLRequired, rex.MissingSchema,
                                  rex.InvalidSchema, rex.InvalidURL)

    if exception.__doc__:
        text = text.format(desc=exception.__doc__)
    else:
        text = text.format(
            desc="An unknown exception was raised. Please report this.")

    # CONNECTION FAILURES
    if isinstance(exception, (rex.ProxyError, rex.SSLError,
                              rex.ChunkedEncodingError, rex.ConnectionError)):
        tags.update(["CONNECTION_FAIL"])
    # TIMEOUTS
    elif isinstance(exception, (rex.ConnectTimeout, rex.ReadTimeout)):
        tags.update(["CONNECTION_TIMEOUT", "SERVER_FAIL"])
    # INVALID REQUESTS
    elif isinstance(exception, invalid_request_exceptions):
        tags.update(["INVALID_REQUEST", "CLIENT_FAIL"])

    return syntribos.signal.SynSignal(
        text=text,
        slug=slug,
        strength=1.0,
        tags=list(tags),
        data=data,
        check_name=check_name) 
Example #21
Source File: FloatGetter.py    From CSGO-Market-Float-Finder with MIT License 4 votes vote down vote up
def getMarketItems(url, count, currency, start=0):
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url

    url = url_fix(url)
    curr = CURRENCY[currency][0]
    urlextender = '/render/?query=&start=%s&count=%s&currency=%s' % (start, count, curr)

    try:
        request = requests.get(url + urlextender)
    except requests.ConnectionError:
        return 'Could not connect. Check URL and make sure you can connect to the internet.', None
    except exceptions.InvalidURL:
        return 'URL is invalid, please check your market URL.', None

    if request.status_code == 404:
        return 'Could not connect to Steam. Retry in a few minutes and check URL.', None
    if len(request.text) < 1000:
        return 'Response from Steam contains no skin data, URL is probably invalid.', None
    if request.url != url + urlextender:
        return 'Page redirected to %s, so no skins were found. Check your market URL.' % request.url, None

    data = request.text.split('"listinginfo":')[1].split(',"assets":')[0]
    try:
        data = json.loads(data, object_pairs_hook=OrderedDict)
    except ValueError:
        return 'Response from Steam contains no skin data, URL is probably invalid.', None

    # assetID => [marketID, inspect link, formatted price]
    datadic = OrderedDict()
    soldcount = 0
    for marketID in data:
        try:
            price = int(data[marketID]['converted_price']) + int(data[marketID]['converted_fee'])
            padded = "%03d" % (price,)
            price = padded[0:-2] + '.' + padded[-2:]
        except KeyError:
            price = 'SOLD'
            soldcount += 1
            continue  # Delete this line to keep SOLD ITEMS in the result
        link = data[marketID]['asset']['market_actions'][0]['link']
        assetID = data[marketID]['asset']['id']
        datadic[assetID] = [marketID, link.replace('%assetid%', assetID).replace('%listingid%', marketID), price]

    return datadic, soldcount 
Example #22
Source File: PackageRequest.py    From FXTest with MIT License 4 votes vote down vote up
def get(self, url,headers,parms):#get消息
        try:
            self.r = requests.get(url, headers=headers,params=parms,timeout=Interface_Time_Out)
            self.r.encoding = 'UTF-8'
            spend=self.r.elapsed.total_seconds()
            json_response = json.loads(self.r.text)
            return json_response,spend
        except exceptions.Timeout :
            return {'get请求出错': "请求超时" }
        except exceptions.InvalidURL:
            return {'get请求出错': "非法url"}
        except exceptions.HTTPError:
            return {'get请求出错': "http请求错误"}
        except Exception as e:
            return {'get请求出错':"错误原因:%s"%e} 
Example #23
Source File: ezstickerbot.py    From ez-sticker-bot with MIT License 4 votes vote down vote up
def url_received(update: Update, context: CallbackContext):
    message = update.message
    user_id = message.from_user.id
    text = message.text.split(' ')

    # check spam filter
    cooldown_info = user_on_cooldown(user_id)
    if cooldown_info[0]:
        message.reply_markdown(get_message(user_id, 'spam_limit_reached').format(cooldown_info[1], cooldown_info[2]))
        return

    if len(text) > 1:
        message.reply_text(get_message(message.chat_id, "too_many_urls"))
        return

    text = text[0]
    url = urlparse(text, 'https').geturl()

    # remove extra backslash after https if it exists
    if url.lower().startswith("https:///"):
        url = url.replace("https:///", "https://", 1)

    # get request
    try:
        request = requests.get(url, timeout=3)
        request.raise_for_status()
    except InvalidURL:
        message.reply_markdown(get_message(message.chat_id, "invalid_url").format(url))
        return
    except HTTPError:
        message.reply_markdown(get_message(message.chat_id, "url_does_not_exist").format(url))
        return
    except Timeout or ConnectTimeout:
        message.reply_markdown(get_message(message.chat_id, "url_timeout").format(url))
        return
    except ConnectionError or RequestException or UnicodeError:
        message.reply_markdown(get_message(message.chat_id, "unable_to_connect").format(url))
        return
    except UnicodeError:
        message.reply_markdown(get_message(message.chat_id, "unable_to_connect").format(url))
        return

    # read image from url
    try:
        image = Image.open(BytesIO(request.content))
    except OSError:
        message.reply_markdown(get_message(message.chat_id, "url_not_img").format(url))
        return

    # feedback to show bot is processing
    bot.send_chat_action(message.chat_id, 'upload_document')

    create_sticker_file(message, image, context) 
Example #24
Source File: hdfs_namenode.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _rest_request_to_json(self, url, object_path, query_params, tags=None):
        """
        Query the given URL and return the JSON response
        """
        if object_path:
            url = self._join_url_dir(url, object_path)

        # Add query_params as arguments
        if query_params:
            query = '&'.join(['{}={}'.format(key, value) for key, value in iteritems(query_params)])
            url = urljoin(url, '?' + query)

        self.log.debug('Attempting to connect to "%s"', url)

        try:
            response = self.http.get(url)
            response.raise_for_status()
            response_json = response.json()

        except Timeout as e:
            self.service_check(
                self.JMX_SERVICE_CHECK, AgentCheck.CRITICAL, tags=tags, message="Request timeout: {}, {}".format(url, e)
            )
            raise

        except (HTTPError, InvalidURL, ConnectionError) as e:
            self.service_check(
                self.JMX_SERVICE_CHECK, AgentCheck.CRITICAL, tags=tags, message="Request failed: {}, {}".format(url, e)
            )
            raise

        except JSONDecodeError as e:
            self.service_check(
                self.JMX_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=tags,
                message='JSON Parse failed: {}, {}'.format(url, e),
            )
            raise

        except ValueError as e:
            self.service_check(self.JMX_SERVICE_CHECK, AgentCheck.CRITICAL, tags=tags, message=str(e))
            raise

        else:
            return response_json 
Example #25
Source File: mapreduce.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _rest_request_to_json(self, address, object_path, service_name=None, tags=None, *args, **kwargs):
        """
        Query the given URL and return the JSON response
        """
        tags = [] if tags is None else tags

        service_check_tags = ['url:{}'.format(self._get_url_base(address))] + tags

        url = address

        if object_path:
            url = self._join_url_dir(url, object_path)

        # Add args to the url
        if args:
            for directory in args:
                url = self._join_url_dir(url, directory)

        self.log.debug('Attempting to connect to "%s"', url)

        # Add kwargs as arguments
        if kwargs:
            query = '&'.join(['{}={}'.format(key, value) for key, value in iteritems(kwargs)])
            url = urljoin(url, '?' + query)

        try:
            response = self.http.get(url)
            response.raise_for_status()
            response_json = response.json()

        except Timeout as e:
            self._critical_service(service_name, service_check_tags, "Request timeout: {}, {}".format(url, e))
            raise

        except (HTTPError, InvalidURL, ConnectionError) as e:
            self._critical_service(service_name, service_check_tags, "Request failed: {}, {}".format(url, e))
            raise

        except JSONDecodeError as e:
            self._critical_service(service_name, service_check_tags, "JSON Parse failed: {}, {}".format(url, e))
            raise

        except ValueError as e:
            self._critical_service(service_name, service_check_tags, str(e))
            raise

        return response_json