Skip to main content

Python implementation of the WHATWG URL Living Standard

Project description


Travis Codecov PyPI

Python implementation of the WHATWG URL Living Standard.

The latest revision that this package implements of the standard is August 7th, 2018 (commit 49060c7)

Getting Started

Install the whatwg-url package using pip.

python -m pip install whatwg-url

And use the module like so:

import whatwg_url

url = whatwg_url.parse_url("")
# Url(scheme='https', hostname='', port=None, path='', query='', fragment='')


Compatibility with urllib.parse.urlparse()

import whatwg_url

parseresult = whatwg_url.urlparse("")

print(parseresult.scheme)  # 'https'
print(parseresult.netloc)  # ''
print(parseresult.userinfo)  # 'seth:larson'
print(parseresult.path)  # '/maps'
print(parseresult.params)  # ''
print(parseresult.query)  # 'query=string'
print(parseresult.fragment)  # 'fragment'
print(parseresult.username)  # 'seth'
print(parseresult.password)  # 'larson'
print(parseresult.hostname)  # ''
print(parseresult.port)  # 1234
print(parseresult.geturl())  # ''

URL Normalization

The WHATWG URL specification describes methods of normalizing URL inputs to usable URLs. It handles percent-encodings, default ports, paths, IPv4 and IPv6 addresses, IDNA (2008 and 2003), multiple slashes after scheme, etc.

import whatwg_url

print(whatwg_url.normalize_url("https://////"))  #
print(whatwg_url.normalize_url(""))  #
print(whatwg_url.normalize_url("https://你好你好"))  # https://xn--6qqa088eba/
print(whatwg_url.normalize_url("https://0Xc0.0250.01"))  #

URL Validation

print(whatwg_url.is_valid_url(""))  # True
print(whatwg_url.is_valid_url("https://www"))  # False

Relative URLs

HTTP redirects often contain relative URLs (via the Location header) that need to be applied to the current URL location. Specifying the base parameter allows for giving relative URLs as input and the changes be applied to a new URL object.

import whatwg_url

url = whatwg_url.parse_url("../dev?a=1#f", base="")
print(url.href)  #

URL Property Mutators

Modifying properties on a URL object use the parser and "state overrides" to properly mutate the URL object.

url = whatwg_url.parse_url("")

print(url.scheme)  # 'http'
print(url.port)  # 443

url.scheme = 'https'

print(url.scheme)  # 'https'
print(url.port)  # None


The module is a single file which allows for easy vendoring into projects.






  • Added UrlParser and Url
  • Added UrlParser.parse_host()
  • Added UrlParser.parse_ipv4_host()
  • Added Url.origin
  • Added Url.authority
  • Added urlparse and urljoin to be compatible with urllib3.parse.urlparse and urllib.parse.urljoin
  • Added support for Python 2.7, 3.4, and 3.5

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

whatwg-url-2018.8.26.tar.gz (30.4 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page