Skip to content

url

validators.url.url(value, /, *, skip_ipv6_addr=False, skip_ipv4_addr=False, may_have_port=True, simple_host=False, strict_query=True, rfc_1034=False, rfc_2782=False)

Return whether or not given value is a valid URL.

This validator was inspired from URL validator of dperini. The following diagram is from urlly.

    foo://admin:hunter1@example.com:8042/over/there?name=ferret#nose
    \_/   \___/ \_____/ \_________/ \__/\_________/ \_________/ \__/
     |      |       |       |        |       |          |         |
  scheme username password hostname port    path      query    fragment

Examples:

>>> url('http://duck.com')
# Output: True
>>> url('ftp://foobar.dk')
# Output: True
>>> url('http://10.0.0.1')
# Output: True
>>> url('http://example.com/">user@example.com')
# Output: ValidationError(func=url, ...)

Parameters:

Name Type Description Default
value str

URL string to validate.

required
skip_ipv6_addr bool

When URL string cannot contain an IPv6 address.

False
skip_ipv4_addr bool

When URL string cannot contain an IPv4 address.

False
may_have_port bool

URL string may contain port number.

True
simple_host bool

URL string maybe only hyphens and alpha-numerals.

False
strict_query bool

Fail validation on query string parsing error.

True
rfc_1034 bool

Allow trailing dot in domain/host name. Ref: RFC 1034.

False
rfc_2782 bool

Domain/Host name is of type service record. Ref: RFC 2782.

False

Returns:

Type Description
Literal[True]

If value is a valid slug.

ValidationError

If value is an invalid slug.

Note
  • In version 0.11.3:
    • Added support for URLs containing localhost.
  • In version 0.11.0:
    • Made the regular expression case insensitive.
  • In version 0.10.3:
    • Added a public parameter.
  • In version 0.10.2:
    • Added support for various exotic URLs.
    • Fixed various false positives.

New in version 0.2.0.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/url.py
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
@validator
def url(
    value: str,
    /,
    *,
    skip_ipv6_addr: bool = False,
    skip_ipv4_addr: bool = False,
    may_have_port: bool = True,
    simple_host: bool = False,
    strict_query: bool = True,
    rfc_1034: bool = False,
    rfc_2782: bool = False,
):
    r"""Return whether or not given value is a valid URL.

    This validator was inspired from [URL validator of dperini][1].
    The following diagram is from [urlly][2].

            foo://admin:hunter1@example.com:8042/over/there?name=ferret#nose
            \_/   \___/ \_____/ \_________/ \__/\_________/ \_________/ \__/
             |      |       |       |        |       |          |         |
          scheme username password hostname port    path      query    fragment

    [1]: https://gist.github.com/dperini/729294
    [2]: https://github.com/treeform/urlly

    Examples:
        >>> url('http://duck.com')
        # Output: True
        >>> url('ftp://foobar.dk')
        # Output: True
        >>> url('http://10.0.0.1')
        # Output: True
        >>> url('http://example.com/">user@example.com')
        # Output: ValidationError(func=url, ...)

    Args:
        value:
            URL string to validate.
        skip_ipv6_addr:
            When URL string cannot contain an IPv6 address.
        skip_ipv4_addr:
            When URL string cannot contain an IPv4 address.
        may_have_port:
            URL string may contain port number.
        simple_host:
            URL string maybe only hyphens and alpha-numerals.
        strict_query:
            Fail validation on query string parsing error.
        rfc_1034:
            Allow trailing dot in domain/host name.
            Ref: [RFC 1034](https://www.rfc-editor.org/rfc/rfc1034).
        rfc_2782:
            Domain/Host name is of type service record.
            Ref: [RFC 2782](https://www.rfc-editor.org/rfc/rfc2782).

    Returns:
        (Literal[True]):
            If `value` is a valid slug.
        (ValidationError):
            If `value` is an invalid slug.

    Note:
        - *In version 0.11.3*:
            - Added support for URLs containing localhost.
        - *In version 0.11.0*:
            - Made the regular expression case insensitive.
        - *In version 0.10.3*:
            - Added a `public` parameter.
        - *In version 0.10.2*:
            - Added support for various exotic URLs.
            - Fixed various false positives.

    > *New in version 0.2.0*.
    """
    if not value or re.search(r"\s", value):
        # url must not contain any white
        # spaces, they must be encoded
        return False

    try:
        scheme, netloc, path, query, fragment = urlsplit(value)
    except ValueError:
        return False

    return (
        _validate_scheme(scheme)
        and _validate_netloc(
            netloc,
            skip_ipv6_addr,
            skip_ipv4_addr,
            may_have_port,
            simple_host,
            rfc_1034,
            rfc_2782,
        )
        and _validate_optionals(path, query, fragment, strict_query)
    )