Package: urlstd
urlstdurlstdis a Python implementation of the WHATWGURL Living Standard.This library providesURLclass,URLSearchParamsclass, and low-level APIs that comply with the URL specification.Supported APIsURL classclass urlstd.parse.URL(url: str, base: Optional[str | URL] = None)canParse: classmethodcan_parse(url: str, base: Optional[str | URL] = None) -> boolstringifier:__str__() -> strhref:readonly property href: strorigin:readonly property origin: strprotocol:property protocol: strusername:property username: strpassword:property password: strhost:property host: strhostname:property hostname: strport:property port: strpathname:property pathname: strsearch:property search: strsearchParams:readonly property search_params: URLSearchParamshash:property hash: strURL equivalence:__eq__(other: Any) -> boolandequals(other: URL, exclude_fragments: bool = False) → boolURLSearchParams classclass urlstd.parse.URLSearchParams(init: Optional[str | Sequence[Sequence[str | int | float]] | dict[str, str | int | float] | URLRecord | URLSearchParams] = None)size:__len__() -> intappend:append(name: str, value: str | int | float) -> Nonedelete:delete(name: str, value: Optional[str | int | float] = None) -> Noneget:get(name: str) -> str | NonegetAll:get_all(name: str) -> tuple[str, ...]has:has(name: str, value: Optional[str | int | float] = None) -> boolset:set(name: str, value: str | int | float) -> Nonesort:sort() -> Noneiterable<USVString, USVString>:__iter__() -> Iterator[tuple[str, str]]stringifier:__str__() -> strLow-level APIsURL parserurlstd.parse.parse_url(urlstring: str, base: Optional[str | URLRecord] = None, encoding: str = "utf-8") -> URLRecordbasic URL parserclass urlstd.parse.BasicURLParserclassmethodparse(urlstring: str, base: Optional[URLRecord] = None, encoding: str = "utf-8", url: Optional[URLRecord] = None, state_override: Optional[URLParserState] = None) -> URLRecordURL recordclass urlstd.parse.URLRecordscheme:property scheme: str = ""username:property username: str = ""password:property password: str = ""host:property host: Optional[str | int | tuple[int, ...]] = Noneport:property port: Optional[int] = Nonepath:property path: list[str] | str = []query:property query: Optional[str] = Nonefragment:property fragment: Optional[str] = Noneorigin:readonly property origin: Origin | Noneis special:is_special() -> boolis not special:is_not_special() -> boolincludes credentials:includes_credentials() -> boolhas an opaque path:has_opaque_path() -> boolcannot have a username/password/port:cannot_have_username_password_port() -> boolURL serializer:serialize_url(exclude_fragment: bool = False) -> strhost serializer:serialize_host() -> strURL path serializer:serialize_path() -> strURL equivalence:__eq__(other: Any) -> boolandequals(other: URLRecord, exclude_fragments: bool = False) → boolHosts (domains and IP addresses)class urlstd.parse.IDNAdomain to ASCII: classmethoddomain_to_ascii(domain: str, be_strict: bool = False) -> strdomain to Unicode: classmethoddomain_to_unicode(domain: str, be_strict: bool = False) -> strclass urlstd.parse.Hosthost parser: classmethodparse(host: str, is_not_special: bool = False) -> str | int | tuple[int, ...]host serializer: classmethodserialize(host: str | int | Sequence[int]) -> strpercent-decode a stringurlstd.parse.string_percent_decode(s: str) -> bytespercent-encode after encodingurlstd.parse.string_percent_encode(s: str, safe: str, encoding: str = "utf-8", space_as_plus: bool = False) -> strapplication/x-www-form-urlencoded parserurlstd.parse.parse_qsl(query: bytes) -> list[tuple[str, str]]application/x-www-form-urlencoded serializerurlstd.parse.urlencode(query: Sequence[tuple[str, str]], encoding: str = "utf-8") -> strValidationclass urlstd.parse.HostValidatorvalid host string: classmethodis_valid(host: str) -> boolvalid domain string: classmethodis_valid_domain(domain: str) -> boolvalid IPv4-address string: classmethodis_valid_ipv4_address(address: str) -> boolvalid IPv6-address string: classmethodis_valid_ipv6_address(address: str) -> boolclass urlstd.parse.URLValidatorvalid URL string: classmethodis_valid(urlstring: str, base: Optional[str | URLRecord] = None, encoding: str = "utf-8") -> boolvalidURL-scheme string: classmethodis_valid_url_scheme(value: str) -> boolCompatibility with standard libraryurlliburlstd.parse.urlparse(urlstring: str, base: str = None, encoding: str = "utf-8", allow_fragments: bool = True) -> urllib.parse.ParseResulturlstd.parse.urlparse()ia an alternative tourllib.parse.urlparse().
Parses a string representation of a URL using the basic URL parser, and returnsurllib.parse.ParseResult.Basic UsageTo parse a string into aURL:fromurlstd.parseimportURLURL('http://user:pass@foo:21/bar;par?b#c')# → <URL(href='http://user:pass@foo:21/bar;par?b#c', origin='http://foo:21', protocol='http:', username='user', password='pass', host='foo:21', hostname='foo', port='21', pathname='/bar;par', search='?b', hash='#c')>To parse a string into aURLwith using a base URL:url=URL('?ﬃ&🌈',base='http://example.org')url# → <URL(href='http://example.org/?%EF%AC%83&%F0%9F%8C%88', origin='http://example.org', protocol='http:', username='', password='', host='example.org', hostname='example.org', port='', pathname='/', search='?%EF%AC%83&%F0%9F%8C%88', hash='')>url.search# → '?%EF%AC%83&%F0%9F%8C%88'params=url.search_paramsparams# → URLSearchParams([('ﬃ', ''), ('🌈', '')])params.sort()params# → URLSearchParams([('🌈', ''), ('ﬃ', '')])url.search# → '?%F0%9F%8C%88=&%EF%AC%83='str(url)# → 'http://example.org/?%F0%9F%8C%88=&%EF%AC%83='To validate a URL string:fromurlstd.parseimportURL,URLValidator,ValidityStateURL.can_parse('https://user:password@example.org/')# → TrueURLValidator.is_valid('https://user:password@example.org/')# → Falsevalidity=ValidityState()URLValidator.is_valid('https://user:password@example.org/',validity=validity)validity.valid# → Falsevalidity.validation_errors# → 1validity.descriptions[0]# → "invalid-credentials: input includes credentials: 'https://user:password@example.org/' at position 21"URL.can_parse('file:///C|/demo')# → TrueURLValidator.is_valid('file:///C|/demo')# → Falsevalidity=ValidityState()URLValidator.is_valid('file:///C|/demo',validity=validity)# → Falsevalidity.valid# → Falsevalidity.validation_errors# → 1validity.descriptions[0]# → "invalid-URL-unit: code point is found that is not a URL unit: U+007C (|) in 'file:///C|/demo' at position 9"To parse a string into aurllib.parse.ParseResultwith using a base URL:importhtmlfromurllib.parseimportunquotefromurlstd.parseimporturlparsepr=urlparse('?aÿb',base='http://example.org/foo/',encoding='utf-8')pr# → ParseResult(scheme='http', netloc='example.org', path='/foo/', params='', query='a%C3%BFb', fragment='')unquote(pr.query)# → 'aÿb'pr=urlparse('?aÿb',base='http://example.org/foo/',encoding='windows-1251')pr# → ParseResult(scheme='http', netloc='example.org', path='/foo/', params='', query='a%26%23255%3Bb', fragment='')unquote(pr.query,encoding='windows-1251')# → 'a&#255;b'html.unescape('a&#255;b')# → 'aÿb'pr=urlparse('?aÿb',base='http://example.org/foo/',encoding='windows-1252')pr# → ParseResult(scheme='http', netloc='example.org', path='/foo/', params='', query='a%FFb', fragment='')unquote(pr.query,encoding='windows-1252')# → 'aÿb'Loggingurlstduses standard libraryloggingforvalidation error.
Change the logger log level ofurlstdif needed:logging.getLogger('urlstd').setLevel(logging.ERROR)Dependenciesicupy>= 0.11.0 (pre-built packagesare available)icupyrequirements:ICU4C(ICU - International Components for Unicode) - latest version recommendedC++17 compatible compiler (seesupported compilers)CMake>= 3.7InstallationConfiguring environment variables for icupy (ICU):Windows:Set theICU_ROOTenvironment variable to the root of the ICU installation (default isC:\icu).
For example, if the ICU is located inC:\icu4c:setICU_ROOT=C:\icu4cor in PowerShell:$env:ICU_ROOT ="C:\icu4c"To verify settings usingicuinfo (64 bit):%ICU_ROOT%\bin64\icuinfoor in PowerShell:&$env:ICU_ROOT\bin64\icuinfoLinux/POSIX:If the ICU is located in a non-regular place, set thePKG_CONFIG_PATHandLD_LIBRARY_PATHenvironment variables.
For example, if the ICU is located in/usr/local:exportPKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATHexportLD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATHTo verify settings usingpkg-config:$pkg-config--cflags--libsicu-uc
-I/usr/local/include-L/usr/local/lib-licuuc-licudataInstalling from PyPI:pipinstallurlstdRunning TestsInstall dependencies:pipxinstalltox# orpipinstall--usertoxTo run tests and generate a report:gitclonehttps://github.com/miute/urlstd.gitcdurlstd
tox-ewptSee result:tests/wpt/report.htmlLicenseMIT License.