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, consider_tld=False, private=None, rfc_1034=False, rfc_2782=False)

Return whether or not given value is a valid URL.

This validator was originally 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
consider_tld bool

Restrict domain to TLDs allowed by IANA.

False
private Optional[bool]

Embedded IP address is public if False, private/local if True.

None
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 url.

ValidationError

If value is an invalid url.

Source code in src/validators/url.py
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
@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,
    consider_tld: bool = False,
    private: Optional[bool] = None,  # only for ip-addresses
    rfc_1034: bool = False,
    rfc_2782: bool = False,
):
    r"""Return whether or not given value is a valid URL.

    This validator was originally 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.
        consider_tld:
            Restrict domain to TLDs allowed by IANA.
        private:
            Embedded IP address is public if `False`, private/local if `True`.
        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 url.
        (ValidationError): If `value` is an invalid url.
    """
    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,
            consider_tld,
            private,
            rfc_1034,
            rfc_2782,
        )
        and _validate_optionals(path, query, fragment, strict_query)
    )