Skip to content

# External

The tool to check the availability or syntax of domain, IP or URL.

::

    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides the extra rules handler based on some DNS records.

Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks: https://pyfunceble.github.io/#/special-thanks

Contributors: https://pyfunceble.github.io/#/contributors

Project link: https://github.com/funilrys/PyFunceble

Project documentation: https://docs.pyfunceble.com

Project homepage: https://pyfunceble.github.io/

License: ::

    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

## `ExternalRulesHandler`

Bases: `ExtraRuleHandlerBase`

Provides the external rules handler that is used to handle the external provided rules.

Through this handler, end-user can provide their own rules to handle the availability status of a subject.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`status` | `Optional[AvailabilityCheckerStatus]` | The previously gathered status. | `None`

Source code in `PyFunceble/checker/availability/extras/external.py`

     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341

    class ExternalRulesHandler(ExtraRuleHandlerBase):
        """
        Provides the external rules handler that is used to handle the external
        provided rules.

        Through this handler, end-user can provide their own rules to handle
        the availability status of a subject.

        :param status:
            The previously gathered status.
        :type status:
            :class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus`
        """

        rulesets: list = []
        """
        The rulesets to process.

        If you want to switch from the status code, you should provide a dict
        with the following structure:

            {
                "subject_pattern": ".*", // The pattern the subject should match.
                "validation_type": "status_code", // Type of validation (status_code, headers, body, etc.)
                "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
                "required_status_code": [404], // Status code to match.
            }

        If you want to switch from the headers, you should provide a dict

            {
                "subject_pattern": ".*", // The pattern the subject should match.
                "validation_type": "headers", // Type of validation (status_code, headers, body, etc.)
                "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
                "required_headers_patterns": { // Required, the headers to match.
                    "header_name": ["possible", "values"]
                },
            }

        If you want to switch from the body, you should provide a dict

            {
                "subject_pattern": ".*", // The pattern the subject should match.
                "validation_type": "body", // Type of validation (status_code, headers, body, etc.)
                "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
                "required_body_patterns": ["regex1", "regex2"] // Required, the body patterns to match.
            }

        If you want to switch from a combination of headers and body, you should provide a dict

            {
                "subject_pattern": ".*", // The pattern the subject should match.
                "validation_type": "headers+body", // Type of validation (status_code, headers, body, etc.)
                "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
                "required_headers_patterns": { // Required, the headers to match.
                    "header_name": ["possible", "values"]
                },
                "required_body_patterns": ["regex1", "regex2"] // Required, the body patterns to match.
            }

        If you want to switch from a combination of all, you should provide a dict

            {
                "subject_pattern": ".*", // The pattern the subject should match.
                "validation_type": "all", // Type of validation (status_code, headers, body, etc.)
                "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
                "required_status_code": [404], // Optional, Status code to match.
                "required_headers_patterns": { // Optional, the headers to match.
                    "header_name": ["possible", "values"]
                },
                "required_body_patterns": ["regex1", "regex2"] // Optional, the body patterns to match.
            }

        """

        def __init__(
            self,
            status: Optional[AvailabilityCheckerStatus] = None,
            *,
            rulesets: list = None
        ) -> None:
            if rulesets is not None:
                self.rulesets = rulesets

            super().__init__(status)

        def switch_from_status_code_rule(self, rule: dict) -> "ExternalRulesHandler":
            """
            Switch from the status code rule.

            :param rule:
                The rule to switch from.
            :type rule: dict
            """

            required_keys = ["validation_type", "required_status_code"]

            if any(x not in rule for x in required_keys):
                return self

            if rule["validation_type"] != "status_code":
                return self

            if all(
                self.status.http_status_code != int(x) for x in rule["required_status_code"]
            ):
                return self

            if rule["state_transition"] == "up":
                return self.switch_to_up()

            if rule["state_transition"] == "down":
                return self.switch_to_down()

            return self

        def switch_from_headers_rule(self, rule: dict) -> "ExternalRulesHandler":
            """
            Switch from the headers rule.

            :param rule:
                The rule to switch from.
            :type rule: dict
            """

            required_keys = ["validation_type", "required_headers_patterns"]

            if any(x not in rule for x in required_keys):
                return self

            if rule["validation_type"] != "headers":
                return self

            if rule["state_transition"] == "up":
                switch_method = self.switch_to_up

            if rule["state_transition"] == "down":
                switch_method = self.switch_to_down

            if "required_headers_patterns" in rule and rule["required_headers_patterns"]:
                # pylint: disable=possibly-used-before-assignment
                self.do_on_header_match(
                    self.req_url,
                    rule["required_headers_patterns"],
                    method=switch_method,
                    strict=False,
                    allow_redirects=False,
                )

            return self

        def switch_from_body_rule(self, rule: dict) -> "ExternalRulesHandler":
            """
            Switch from the body rule.

            :param rule:
                The rule to switch from.
            :type rule: dict
            """

            required_keys = ["validation_type", "required_body_patterns"]

            if any(x not in rule for x in required_keys):
                return self

            if rule["validation_type"] != "body":
                return self

            if rule["state_transition"] == "up":
                switch_method = self.switch_to_up

            if rule["state_transition"] == "down":
                switch_method = self.switch_to_down

            if "required_body_patterns" in rule and rule["required_body_patterns"]:
                # pylint: disable=possibly-used-before-assignment
                self.do_on_body_match(
                    self.req_url,
                    rule["required_body_patterns"],
                    method=switch_method,
                    strict=False,
                    allow_redirects=False,
                )

            return self

        def switch_from_all_rule(self, rule: dict) -> "ExternalRulesHandler":
            """
            Switch from the all rule.

            :param rule:
                The rule to switch from.
            :type rule: dict
            """

            required_keys = [
                "validation_type",
            ]

            if any(x not in rule for x in required_keys):
                return self

            if rule["validation_type"] != "all":
                return self

            if rule["state_transition"] == "up":
                switch_method = self.switch_to_up

            if rule["state_transition"] == "down":
                switch_method = self.switch_to_down

            if (
                "required_status_code" in rule
                and rule["required_status_code"]
                and any(
                    self.status.http_status_code == int(x)
                    for x in rule["required_status_code"]
                )
            ):
                # pylint: disable=possibly-used-before-assignment
                switch_method()

            if "required_headers_patterns" in rule and rule["required_headers_patterns"]:
                self.do_on_header_match(
                    self.req_url,
                    rule["required_headers_patterns"],
                    method=switch_method,
                    strict=False,
                    allow_redirects=False,
                )

            if "required_body_patterns" in rule and rule["required_body_patterns"]:
                self.do_on_body_match(
                    self.req_url,
                    rule["required_body_patterns"],
                    method=switch_method,
                    strict=False,
                    allow_redirects=False,
                )

            return self

        @ExtraRuleHandlerBase.ensure_status_is_given
        @ExtraRuleHandlerBase.setup_status_before
        @ExtraRuleHandlerBase.setup_status_after
        def start(self) -> "ExternalRulesHandler":
            """
            Process the check and handling of the external rules for the given subject.
            """

            required_keys = ["subject_pattern", "validation_type", "state_transition"]

            for rule in self.rulesets:
                if any(x not in rule for x in required_keys):
                    continue

                if not self.regex_helper.set_regex(rule["subject_pattern"]).match(
                    self.status.netloc, return_match=False
                ):
                    continue

                if rule["state_transition"] not in ["up", "down"]:
                    continue

                if self.status.status_after_extra_rules:
                    # We already switched the status.
                    break

                if rule["validation_type"] == "status_code":
                    self.switch_from_status_code_rule(rule)
                elif rule["validation_type"] == "headers":
                    self.switch_from_headers_rule(rule)
                elif rule["validation_type"] == "body":
                    self.switch_from_body_rule(rule)
                elif rule["validation_type"] == "headers+body":
                    self.switch_from_headers_rule(rule)
                    self.switch_from_body_rule(rule)
                elif rule["validation_type"] == "all":
                    self.switch_from_all_rule(rule)

            return self

### `rulesets = []` `class-attribute` `instance-attribute`

The rulesets to process.

If you want to switch from the status code, you should provide a dict with the following structure:

    {
        "subject_pattern": ".*", // The pattern the subject should match.
        "validation_type": "status_code", // Type of validation (status_code, headers, body, etc.)
        "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
        "required_status_code": [404], // Status code to match.
    }

If you want to switch from the headers, you should provide a dict

    {
        "subject_pattern": ".*", // The pattern the subject should match.
        "validation_type": "headers", // Type of validation (status_code, headers, body, etc.)
        "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
        "required_headers_patterns": { // Required, the headers to match.
            "header_name": ["possible", "values"]
        },
    }

If you want to switch from the body, you should provide a dict

    {
        "subject_pattern": ".*", // The pattern the subject should match.
        "validation_type": "body", // Type of validation (status_code, headers, body, etc.)
        "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
        "required_body_patterns": ["regex1", "regex2"] // Required, the body patterns to match.
    }

If you want to switch from a combination of headers and body, you should provide a dict

    {
        "subject_pattern": ".*", // The pattern the subject should match.
        "validation_type": "headers+body", // Type of validation (status_code, headers, body, etc.)
        "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
        "required_headers_patterns": { // Required, the headers to match.
            "header_name": ["possible", "values"]
        },
        "required_body_patterns": ["regex1", "regex2"] // Required, the body patterns to match.
    }

If you want to switch from a combination of all, you should provide a dict

    {
        "subject_pattern": ".*", // The pattern the subject should match.
        "validation_type": "all", // Type of validation (status_code, headers, body, etc.)
        "state_transition": "up", // "up" -> ACTIVE, "down" -> INACTIVE
        "required_status_code": [404], // Optional, Status code to match.
        "required_headers_patterns": { // Optional, the headers to match.
            "header_name": ["possible", "values"]
        },
        "required_body_patterns": ["regex1", "regex2"] // Optional, the body patterns to match.
    }

### `start()`

Process the check and handling of the external rules for the given subject.

Source code in `PyFunceble/checker/availability/extras/external.py`

    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341

    @ExtraRuleHandlerBase.ensure_status_is_given
    @ExtraRuleHandlerBase.setup_status_before
    @ExtraRuleHandlerBase.setup_status_after
    def start(self) -> "ExternalRulesHandler":
        """
        Process the check and handling of the external rules for the given subject.
        """

        required_keys = ["subject_pattern", "validation_type", "state_transition"]

        for rule in self.rulesets:
            if any(x not in rule for x in required_keys):
                continue

            if not self.regex_helper.set_regex(rule["subject_pattern"]).match(
                self.status.netloc, return_match=False
            ):
                continue

            if rule["state_transition"] not in ["up", "down"]:
                continue

            if self.status.status_after_extra_rules:
                # We already switched the status.
                break

            if rule["validation_type"] == "status_code":
                self.switch_from_status_code_rule(rule)
            elif rule["validation_type"] == "headers":
                self.switch_from_headers_rule(rule)
            elif rule["validation_type"] == "body":
                self.switch_from_body_rule(rule)
            elif rule["validation_type"] == "headers+body":
                self.switch_from_headers_rule(rule)
                self.switch_from_body_rule(rule)
            elif rule["validation_type"] == "all":
                self.switch_from_all_rule(rule)

        return self

### `switch_from_all_rule(rule)`

Switch from the all rule.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`rule` | `dict` | The rule to switch from. | required

Source code in `PyFunceble/checker/availability/extras/external.py`

    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301

    def switch_from_all_rule(self, rule: dict) -> "ExternalRulesHandler":
        """
        Switch from the all rule.

        :param rule:
            The rule to switch from.
        :type rule: dict
        """

        required_keys = [
            "validation_type",
        ]

        if any(x not in rule for x in required_keys):
            return self

        if rule["validation_type"] != "all":
            return self

        if rule["state_transition"] == "up":
            switch_method = self.switch_to_up

        if rule["state_transition"] == "down":
            switch_method = self.switch_to_down

        if (
            "required_status_code" in rule
            and rule["required_status_code"]
            and any(
                self.status.http_status_code == int(x)
                for x in rule["required_status_code"]
            )
        ):
            # pylint: disable=possibly-used-before-assignment
            switch_method()

        if "required_headers_patterns" in rule and rule["required_headers_patterns"]:
            self.do_on_header_match(
                self.req_url,
                rule["required_headers_patterns"],
                method=switch_method,
                strict=False,
                allow_redirects=False,
            )

        if "required_body_patterns" in rule and rule["required_body_patterns"]:
            self.do_on_body_match(
                self.req_url,
                rule["required_body_patterns"],
                method=switch_method,
                strict=False,
                allow_redirects=False,
            )

        return self

### `switch_from_body_rule(rule)`

Switch from the body rule.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`rule` | `dict` | The rule to switch from. | required

Source code in `PyFunceble/checker/availability/extras/external.py`

    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245

    def switch_from_body_rule(self, rule: dict) -> "ExternalRulesHandler":
        """
        Switch from the body rule.

        :param rule:
            The rule to switch from.
        :type rule: dict
        """

        required_keys = ["validation_type", "required_body_patterns"]

        if any(x not in rule for x in required_keys):
            return self

        if rule["validation_type"] != "body":
            return self

        if rule["state_transition"] == "up":
            switch_method = self.switch_to_up

        if rule["state_transition"] == "down":
            switch_method = self.switch_to_down

        if "required_body_patterns" in rule and rule["required_body_patterns"]:
            # pylint: disable=possibly-used-before-assignment
            self.do_on_body_match(
                self.req_url,
                rule["required_body_patterns"],
                method=switch_method,
                strict=False,
                allow_redirects=False,
            )

        return self

### `switch_from_headers_rule(rule)`

Switch from the headers rule.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`rule` | `dict` | The rule to switch from. | required

Source code in `PyFunceble/checker/availability/extras/external.py`

    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210

    def switch_from_headers_rule(self, rule: dict) -> "ExternalRulesHandler":
        """
        Switch from the headers rule.

        :param rule:
            The rule to switch from.
        :type rule: dict
        """

        required_keys = ["validation_type", "required_headers_patterns"]

        if any(x not in rule for x in required_keys):
            return self

        if rule["validation_type"] != "headers":
            return self

        if rule["state_transition"] == "up":
            switch_method = self.switch_to_up

        if rule["state_transition"] == "down":
            switch_method = self.switch_to_down

        if "required_headers_patterns" in rule and rule["required_headers_patterns"]:
            # pylint: disable=possibly-used-before-assignment
            self.do_on_header_match(
                self.req_url,
                rule["required_headers_patterns"],
                method=switch_method,
                strict=False,
                allow_redirects=False,
            )

        return self

### `switch_from_status_code_rule(rule)`

Switch from the status code rule.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`rule` | `dict` | The rule to switch from. | required

Source code in `PyFunceble/checker/availability/extras/external.py`

    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175

    def switch_from_status_code_rule(self, rule: dict) -> "ExternalRulesHandler":
        """
        Switch from the status code rule.

        :param rule:
            The rule to switch from.
        :type rule: dict
        """

        required_keys = ["validation_type", "required_status_code"]

        if any(x not in rule for x in required_keys):
            return self

        if rule["validation_type"] != "status_code":
            return self

        if all(
            self.status.http_status_code != int(x) for x in rule["required_status_code"]
        ):
            return self

        if rule["state_transition"] == "up":
            return self.switch_to_up()

        if rule["state_transition"] == "down":
            return self.switch_to_down()

        return self