Python requests.exceptions.MissingSchema() Examples

The following are 28 code examples of requests.exceptions.MissingSchema(). 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: artifactory.py    From skelebot with MIT License 6 votes vote down vote up
def pushArtifact(artifactFile, user, token, file, url, force):
    """Pushes the given file to the url with the provided user/token auth"""

    # Error and exit if artifact already exists and we are not forcing an override
    try:
        if (not force) and (artifactory.ArtifactoryPath(url, auth=(user, token)).exists()):
            raise RuntimeError(ERROR_ALREADY_PUSHED)
    except MissingSchema:
        pass

    # Rename artifact, deploy the renamed artifact, and then rename it back to original name
    print("Deploying {file} to {url}".format(file=file, url=url))
    path = artifactory.ArtifactoryPath(url, auth=(user, token))
    shutil.copyfile(artifactFile, file)
    try:
        path.deploy_file(file)
        os.remove(file)
    except:
        os.remove(file)
        raise 
Example #2
Source File: __init__.py    From reddit2telegram with MIT License 6 votes vote down vote up
def md5_sum_from_url(url):
    try:
        r = requests.get(url, stream=True)
    except InvalidSchema:
        return None
    except MissingSchema:
        return None
    chunk_counter = 0
    hash_store = hashlib.md5()
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:  # filter out keep-alive new chunks
            hash_store.update(chunk)
            chunk_counter += 1
            # It is not possible to send greater than 50 MB via Telegram
            if chunk_counter > 50 * 1024:
                return None
    return hash_store.hexdigest() 
Example #3
Source File: goes_imager_nc.py    From satpy with GNU General Public License v3.0 6 votes vote down vote up
def _load_url_or_file(self, url):
        import requests
        from requests.exceptions import MissingSchema

        try:
            response = requests.get(url)
            if response.ok:
                return response.text
            else:
                raise requests.HTTPError
        except (MissingSchema, requests.HTTPError):
            # Not a valid URL, is it a file?
            try:
                return open(url, mode='r')
            except IOError:
                raise ValueError('Invalid URL or file: {}'.format(url)) 
Example #4
Source File: test_http.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_get_request_with_port(self, mock_requests, request_mock, mock_session):
        from requests.exceptions import MissingSchema

        with mock.patch(
            'airflow.hooks.base_hook.BaseHook.get_connection',
            side_effect=get_airflow_connection_with_port
        ):
            expected_url = 'http://test.com:1234/some/endpoint'
            for endpoint in ['some/endpoint', '/some/endpoint']:

                try:
                    self.get_hook.run(endpoint)
                except MissingSchema:
                    pass

                request_mock.assert_called_once_with(
                    mock.ANY,
                    expected_url,
                    headers=mock.ANY,
                    params=mock.ANY
                )

                request_mock.reset_mock() 
Example #5
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 #6
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 #7
Source File: subset_base.py    From flyingpigeon with Apache License 2.0 5 votes vote down vote up
def is_opendap_url(url):
    """
    Check if a provided url is an OpenDAP url.
    The DAP Standard specifies that a specific tag must be included in the
    Content-Description header of every request. This tag is one of:
        "dods-dds" | "dods-das" | "dods-data" | "dods-error"
    So we can check if the header starts with `dods`.
    Even then, some OpenDAP servers seem to not include the specified header...
    So we need to let the netCDF4 library actually open the file.
    """
    from requests.exceptions import MissingSchema, InvalidSchema
    from requests.exceptions import ConnectionError as reqCE

    try:
        content_description = requests.head(url, timeout=5).headers.get("Content-Description")
    except (ConnectionError, reqCE, MissingSchema, InvalidSchema):
        return False

    if content_description:
        return content_description.lower().startswith("dods")
    else:
        try:
            dataset = nc.Dataset(url)
        except OSError:
            return False
        return dataset.disk_format in ('DAP2', 'DAP4') 
Example #8
Source File: test_utils.py    From AnyAPI with MIT License 5 votes vote down vote up
def test_retry():
    """Test retry utility"""
    # I know that I should test retry and retry_until separately
    # But I couldn't find any idea to test retry_until separately
    try:
        invalid_api = AnyAPI("invalidurl", scoped_calls=[retry(2)])
        invalid_api.GET()
    except MissingSchema:
        assert True
    else:
        assert False 
Example #9
Source File: SimpleChatClient.py    From matrix-python-sdk with Apache License 2.0 5 votes vote down vote up
def main(host, username, password, room_id_alias):
    client = MatrixClient(host)

    try:
        client.login(username, password)
    except MatrixRequestError as e:
        print(e)
        if e.code == 403:
            print("Bad username or password.")
            sys.exit(4)
        else:
            print("Check your sever details are correct.")
            sys.exit(2)
    except MissingSchema as e:
        print("Bad URL format.")
        print(e)
        sys.exit(3)

    try:
        room = client.join_room(room_id_alias)
    except MatrixRequestError as e:
        print(e)
        if e.code == 400:
            print("Room ID/Alias in the wrong format")
            sys.exit(11)
        else:
            print("Couldn't find room.")
            sys.exit(12)

    room.add_listener(on_message)
    client.start_listener_thread()

    while True:
        msg = samples_common.get_input()
        if msg == "/quit":
            break
        else:
            room.send_text(msg) 
Example #10
Source File: UserPassOrTokenClient.py    From matrix-python-sdk with Apache License 2.0 5 votes vote down vote up
def example(host, user, password, token):
    """run the example."""
    client = None
    try:
        if token:
            print('token login')
            client = MatrixClient(host, token=token)
        else:
            print('password login')
            client = MatrixClient(host)
            token = client.login(user, password)
            print('got token: %s' % token)
    except MatrixRequestError as e:
        print(e)
        if e.code == 403:
            print("Bad username or password")
            exit(2)
        elif e.code == 401:
            print("Bad username or token")
            exit(3)
        else:
            print("Verify server details.")
            exit(4)
    except MissingSchema as e:
        print(e)
        print("Bad formatting of URL.")
        exit(5)
    except InvalidSchema as e:
        print(e)
        print("Invalid URL schema")
        exit(6)
    print("is in rooms")
    for room_id, room in client.get_rooms().items():
        print(room_id) 
Example #11
Source File: test_ensembl_rest_clients.py    From scout with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_send_request_fakey_url(ensembl_rest_client_37):
    """Successful requests are tested by other tests in this file.
       This test will trigger errors instead.
    """
    # GIVEN a completely invalid URL
    url = "fakeyurl"
    # GIVEN a client
    client = ensembl_rest_client_37
    responses.add(
        responses.GET, url, body=MissingSchema(), status=404,
    )
    data = client.send_request(url)
    assert isinstance(data, MissingSchema) 
Example #12
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_schema(instrument, elasticapm_client):
    elasticapm_client.begin_transaction("transaction.test")
    with capture_span("test_request", "test"):
        with pytest.raises(MissingSchema):
            requests.get("") 
Example #13
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_none(instrument, elasticapm_client):
    elasticapm_client.begin_transaction("transaction.test")
    with capture_span("test_request", "test"):
        with pytest.raises(MissingSchema):
            requests.get(None) 
Example #14
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 #15
Source File: matrix_bridge.py    From python-zulip-api with Apache License 2.0 5 votes vote down vote up
def matrix_login(matrix_client: Any, matrix_config: Dict[str, Any]) -> None:
    try:
        matrix_client.login_with_password(matrix_config["username"],
                                          matrix_config["password"])
    except MatrixRequestError as exception:
        if exception.code == 403:
            raise Bridge_FatalMatrixException("Bad username or password.")
        else:
            raise Bridge_FatalMatrixException("Check if your server details are correct.")
    except MissingSchema:
        raise Bridge_FatalMatrixException("Bad URL format.") 
Example #16
Source File: _devices.py    From pyquil with Apache License 2.0 5 votes vote down vote up
def _get_raw_lattice_data(lattice_name: Optional[str] = None) -> Dict[str, str]:
    """
    Produces a dictionary of raw data for a lattice as queried from the Forest 2.0 server.

    Returns a dictionary of the form::

        {
            "name":        the name of the lattice as a string,
            "device_name": the name of the device, given as a string, that the lattice lies on,
            "specs":       a Specs object, serialized as a dictionary,
            "isa":         an ISA object, serialized as a dictionary,
            "noise_model": a NoiseModel object, serialized as a dictionary
        }
    """

    config = PyquilConfig()
    session = get_session(config=config)

    try:
        res = get_json(session, f"{config.forest_url}/lattices/{lattice_name}")
    except MissingSchema:
        raise ValueError(
            f"Error finding lattice `{lattice_name}` at Forest 2.0 server "
            f"""endpoint `{config.forest_url}`.

    Most likely, you're missing an address for the Forest 2.0 server endpoint, or the
    address is invalid. This can be set through the environment variable FOREST_URL or
    by changing the following lines in the QCS config file (by default, at ~/.qcs_config)::

       [Rigetti Forest]
       url = https://rigetti.com/valid/forest/url"""
        )
    return cast(Dict[str, str], res["lattice"]) 
Example #17
Source File: test_http_checks.py    From syntribos with Apache License 2.0 5 votes vote down vote up
def test_missing_schema(self):
        signal = http_checks.check_fail(rex.MissingSchema())
        self._assert_has_tags(self.bad_request_tags, signal)
        self._assert_has_slug("HTTP_FAIL_MISSING_SCHEMA", signal) 
Example #18
Source File: scheduling.py    From eNMS with GNU General Public License v3.0 5 votes vote down vote up
def time_before_next_run(self):
        try:
            return get(
                f"{app.scheduler_address}/time_left/{self.id}", timeout=0.01
            ).json()
        except (ConnectionError, MissingSchema, ReadTimeout):
            return "Scheduler Unreachable" 
Example #19
Source File: scheduling.py    From eNMS with GNU General Public License v3.0 5 votes vote down vote up
def next_run_time(self):
        try:
            return get(
                f"{app.scheduler_address}/next_runtime/{self.id}", timeout=0.01
            ).json()
        except (ConnectionError, MissingSchema, ReadTimeout):
            return "Scheduler Unreachable" 
Example #20
Source File: SimpleChatClient.py    From matrixcli with GNU General Public License v3.0 5 votes vote down vote up
def main(host, username, password, room_id_alias):
    client = MatrixClient(host)

    try:
        client.login_with_password(username, password)
    except MatrixRequestError as e:
        print(e)
        if e.code == 403:
            print("Bad username or password.")
            sys.exit(4)
        else:
            print("Check your sever details are correct.")
            sys.exit(2)
    except MissingSchema as e:
        print("Bad URL format.")
        print(e)
        sys.exit(3)

    try:
        room = client.join_room(room_id_alias)
    except MatrixRequestError as e:
        print(e)
        if e.code == 400:
            print("Room ID/Alias in the wrong format")
            sys.exit(11)
        else:
            print("Couldn't find room.")
            sys.exit(12)

    room.add_listener(on_message)
    client.start_listener_thread()

    while True:
        msg = samples_common.get_input()
        if msg == "/quit":
            break
        else:
            room.send_text(msg) 
Example #21
Source File: UserPassOrTokenClient.py    From matrixcli with GNU General Public License v3.0 5 votes vote down vote up
def example(host, user, password, token):
    """run the example."""
    client = None
    try:
        if token:
            print('token login')
            client = MatrixClient(host, token=token, user_id=user)
        else:
            print('password login')
            client = MatrixClient(host)
            token = client.login_with_password(user, password)
            print('got token: %s' % token)
    except MatrixRequestError as e:
        print(e)
        if e.code == 403:
            print("Bad username or password")
            exit(2)
        elif e.code == 401:
            print("Bad username or token")
            exit(3)
        else:
            print("Verify server details.")
            exit(4)
    except MissingSchema as e:
        print(e)
        print("Bad formatting of URL.")
        exit(5)
    except InvalidSchema as e:
        print(e)
        print("Invalid URL schema")
        exit(6)
    print("is in rooms")
    for room_id, room in client.get_rooms().items():
        print(room_id) 
Example #22
Source File: utils.py    From scrape with MIT License 5 votes vote down vote up
def get_raw_resp(url):
    """Get webpage response as a unicode string."""
    try:
        headers = {"User-Agent": random.choice(USER_AGENTS)}
        try:
            request = requests.get(url, headers=headers, proxies=get_proxies())
        except MissingSchema:
            url = add_protocol(url)
            request = requests.get(url, headers=headers, proxies=get_proxies())
        return request.text.encode("utf-8") if PY2 else request.text
    except Exception:
        sys.stderr.write("Failed to retrieve {0} as str.\n".format(url))
        raise 
Example #23
Source File: utils.py    From scrape with MIT License 5 votes vote down vote up
def get_resp(url):
    """Get webpage response as an lxml.html.HtmlElement object."""
    try:
        headers = {"User-Agent": random.choice(USER_AGENTS)}
        try:
            request = requests.get(url, headers=headers, proxies=get_proxies())
        except MissingSchema:
            url = add_protocol(url)
            request = requests.get(url, headers=headers, proxies=get_proxies())
        return lh.fromstring(request.text.encode("utf-8") if PY2 else request.text)
    except Exception:
        sys.stderr.write("Failed to retrieve {0}.\n".format(url))
        raise 
Example #24
Source File: test_slack_webhook.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_url_generated_by_http_conn_id_and_endpoint(self, mock_request, mock_session):
        hook = SlackWebhookHook(http_conn_id='slack-webhook-host',
                                webhook_token='B000/XXX')
        try:
            hook.execute()
        except MissingSchema:
            pass
        mock_request.assert_called_once_with(
            self.expected_method,
            self.expected_url,
            headers=mock.ANY,
            data=mock.ANY
        )
        mock_request.reset_mock() 
Example #25
Source File: test_slack_webhook.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_url_generated_by_endpoint(self, mock_request, mock_session):
        hook = SlackWebhookHook(webhook_token=self.expected_url)
        try:
            hook.execute()
        except MissingSchema:
            pass
        mock_request.assert_called_once_with(
            self.expected_method,
            self.expected_url,
            headers=mock.ANY,
            data=mock.ANY
        )
        mock_request.reset_mock() 
Example #26
Source File: test_slack_webhook.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_url_generated_by_http_conn_id(self, mock_request, mock_session):
        hook = SlackWebhookHook(http_conn_id='slack-webhook-url')
        try:
            hook.execute()
        except MissingSchema:
            pass
        mock_request.assert_called_once_with(
            self.expected_method,
            self.expected_url,
            headers=mock.ANY,
            data=mock.ANY
        )
        mock_request.reset_mock() 
Example #27
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 #28
Source File: matrix.py    From fishroom with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, server, username, password, rooms, nick=None):
        client = MatrixClient(server)
        self.viewer_url = server.strip('/') + "/_matrix/media/v1/download/"

        try:
            client.login_with_password(username, password)
        except MatrixRequestError as e:
            if e.code == 403:
                logger.error("403 Bad username or password.")
                sys.exit(4)
            else:
                logger.error("{} Check your server details are correct.".format(e))
                sys.exit(2)
        except MissingSchema as e:
            logger.error("{} Bad URL format.".format(e))
            sys.exit(3)

        self.username = client.user_id
        logger.info("logged in as: {}".format(self.username))

        if nick is not None:
            u = client.get_user(client.user_id)
            logger.info("Setting display name to {}".format(nick))
            try:
                u.set_display_name(nick)
            except MatrixRequestError as e:
                logger.error("Fail to set display name: error = {}".format(e))

        self.joined_rooms = {}
        self.room_id_to_alias = {}
        self.displaynames = {}

        for room_id_alias in rooms:
            try:
                room = client.join_room(room_id_alias)
            except MatrixRequestError as e:
                if e.code == 400:
                    logger.error("400 Room ID/Alias in the wrong format")
                    sys.exit(11)
                else:
                    logger.error("{} Couldn't find room {}".format(e, room_id_alias))
                    sys.exit(12)
            logger.info("Joined room {}".format(room_id_alias))
            self.joined_rooms[room_id_alias] = room
            self.room_id_to_alias[room.room_id] = room_id_alias
            room.add_listener(self.on_message)

        self.client = client
        self.bot_msg_pattern = config['matrix'].get('bot_msg_pattern', None)