Pyjo.URL - Uniform Resource Locator

from Pyjo.URL import URL

# Parse
url = URL('http://sri:foo@example.com:3000/foo?foo=bar#23')
print(url.scheme)
print(url.userinfo)
print(url.host)
print(url.port)
print(url.path)
print(url.query)
print(url.fragment)

# Or import the module, as in Google Python Style Guide

import Pyjo.URL

# Parse
url = Pyjo.URL.new('http://sri:foo@example.com:3000/foo?foo=bar#23')
print(url.scheme)
print(url.userinfo)
print(url.host)
print(url.port)
print(url.path)
print(url.query)
print(url.fragment)

# Build
url = Pyjo.URL.new()
url.scheme = 'http'
url.host = 'example.com'
url.port = 3000
url.path = '/foo/bar'
url.query = ('foo', 'bar')
url.fragment = '23'
print(url)

Pyjo.URL implements a subset of RFC 3986, RFC 3987 and the URL Living Standard for Uniform Resource Locators with support for IDNA and IRIs.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::URL of Mojolicious 9.49.

Classes

class Pyjo.URL.Pyjo_URL(url: str | bytes | UrllibResult | None = None)[source]

Pyjo.URL inherits all methods from Pyjo.Base and Pyjo.String.Mixin and implements the following new ones.

__bool__() → bool[source]
boolean = bool(url)

Always true.

__str__() → str[source]
string = str(url)

Alias for to_str().

property base: Pyjo_URL[source]
base = url.base
url.base = Pyjo.URL.new()

Base of this URL, defaults to a Pyjo.URL object.

# "http://example.com/a/b?c"
Pyjo.URL.new('/a/b?c').set(base=Pyjo.URL.new('http://example.com')).to_abs()
clone() → Self[source]
url2 = url.clone()

Return a new Pyjo.URL object cloned from this URL.

fragment: str | None
fragment = url.fragment
url.fragment = '♥pyjoyment♥'

Fragment part of this URL.

# "yada"
Pyjo.URL.new('http://example.com/foo?bar=baz#yada').fragment
host: str | None
host = url.host
url.host = '127.0.0.1'

Host part of this URL.

# "example.com"
Pyjo.URL.new('http://sri:t3st@example.com:8080/foo').host
property host_port: str | None[source]
host_port = url.host_port
url.host_port = 'example.com:8080'

Normalized version of host and port.

# "xn--n3h.net:8080"
Pyjo.URL.new('http://☃.net:8080/test').host_port

# "example.com"
Pyjo.URL.new('http://example.com/test').host_port
property ihost: str | None[source]
ihost = url.ihost
url.ihost = 'xn--bcher-kva.ch'

Host part of this URL in punycode format.

# "xn--n3h.net"
Pyjo.URL.new('http://☃.net').ihost

# "example.com"
Pyjo.URL.new('http://example.com').ihost
property is_abs: bool[source]
boolean = url.is_abs

Check if URL is absolute.

# True
Pyjo.URL.new('http://example.com').is_abs
Pyjo.URL.new('http://example.com/test/index.html').is_abs

# False
Pyjo.URL.new('test/index.html').is_abs
Pyjo.URL.new('/test/index.html').is_abs
Pyjo.URL.new('//example.com/test/index.html').is_abs
parse(url: str | bytes | UrllibResult) → Self[source]
url = url.parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo')
url = url.parse(urlsplit('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo'))

Parse relative or absolute URL. Bytes are characters 0-255, as a Perl byte string, and a result of urllib.parse.urlsplit() or urllib.parse.urlparse() is taken as the URL it holds.

# "/test/123"
Pyjo.URL.new().parse('/test/123?foo=bar').path

# "example.com"
Pyjo.URL.new().parse('http://example.com/test/123?foo=bar').host

# "sri@example.com"
Pyjo.URL.new().parse('mailto:sri@example.com').path
property password: str | None[source]
password = url.password

Password part of userinfo.

# "s3cret"
Pyjo.URL.new('http://isabel:s3cret@pyjoyment.org').password

# "s:3:c:r:e:t"
Pyjo.URL.new('http://isabel:s:3:c:r:e:t@pyjoyment.org').password
property path: Pyjo_Path[source]
path = url.path
url.path = 'foo/bar'
url.path = '/foo/bar'
url.path = Pyjo.Path.new()

Path part of this URL, relative paths will be merged with Pyjo.Path.Pyjo_Path.merge(), defaults to a Pyjo.Path object.

# "test"
Pyjo.URL.new('http://example.com/test/Pyjo').path.parts[0]

# "/test/DOM/HTML"
Pyjo.URL.new('http://example.com/test/Pyjo').path.merge('DOM/HTML')

# "http://example.com/DOM/HTML"
Pyjo.URL.new('http://example.com/test/Pyjo').set(path='/DOM/HTML')

# "http://example.com/test/DOM/HTML"
Pyjo.URL.new('http://example.com/test/Pyjo').set(path='DOM/HTML')

# "http://example.com/test/Pyjo/DOM/HTML"
Pyjo.URL.new('http://example.com/test/Pyjo/').set(path='DOM/HTML')
property path_query: str[source]
path_query = url.path_query
url.path_query = '/foo/bar?a=1&b=2'

Normalized version of path and query.

# "/test?a=1&b=2"
Pyjo.URL.new('http://example.com/test?a=1&b=2').path_query

# "/"
Pyjo.URL.new('http://example.com/').path_query
port: int | None
port = url.port
url.port = 8080

Port part of this URL.

# 8080
Pyjo.URL.new('http://sri:t3st@example.com:8080/foo').port
property protocol: str[source]
proto = url.protocol

Normalized version of scheme.

# "http"
Pyjo.URL.new('HtTp://example.com').protocol
property query: Pyjo_Parameters[source]
query = url.query
url.query = {'merge': 'to'}
url.query = ['append', 'with']
url.query = ('replace', 'with')
url.query = 'a=1&b=2'
url.query = Pyjo.Parameters.new()

Query part of this URL, key/value pairs in a list will be appended with Pyjo.Parameters.Pyjo_Parameters.append(), key/value pairs in a dict merged with Pyjo.Parameters.Pyjo_Parameters.merge(), and key/value pairs in a tuple replace the parameters, defaults to a Pyjo.Parameters object.

# "2"
Pyjo.URL.new('http://example.com?a=1&b=2').query.param('b')

# "b=2&a=2&c=3"
Pyjo.URL.new('http://example.com?a=1&b=2').query.merge('a', 2, 'c', 3)

# "http://example.com?a=2&c=3"
Pyjo.URL.new('http://example.com?a=1&b=2').set(query=('a', 2, 'c', 3))

# "http://example.com?a=2&a=3"
Pyjo.URL.new('http://example.com?a=1&b=2').set(query=('a', [2, 3]))

# "http://example.com?b=2&a=2&c=3"
Pyjo.URL.new('http://example.com?a=1&b=2').set(query={'a': 2, 'c': 3})

# "http://example.com?b=2"
Pyjo.URL.new('http://example.com?a=1&b=2').set(query={'a': None})

# "http://example.com?a=1&b=2&a=2&c=3"
Pyjo.URL.new('http://example.com?a=1&b=2').set(query=['a', 2, 'c', 3])
scheme: str | None
scheme = url.scheme
url.scheme = 'http'

Scheme part of this URL.

# "http"
Pyjo.URL.new('http://example.com/foo').scheme
to_abs(base: Pyjo_URL | None = None) → Self[source]
abs = url.to_abs()
abs = url.to_abs(Pyjo.URL.new('http://example.com/foo'))

Return a new Pyjo.URL object cloned from this relative URL and turn it into an absolute one using base or provided base URL.

# "http://example.com/foo/baz.xml?test=123"
Pyjo.URL.new('baz.xml?test=123').to_abs(Pyjo.URL.new('http://example.com/foo/bar.html'))

# "http://example.com/baz.xml?test=123"
Pyjo.URL.new('/baz.xml?test=123').to_abs(Pyjo.URL.new('http://example.com/foo/bar.html'))

# "http://example.com/foo/baz.xml?test=123"
Pyjo.URL.new('//example.com/foo/baz.xml?test=123').to_abs(Pyjo.URL.new('http://example.com/foo/bar.html'))
to_str() → str[source]
string = url.to_str()

Turn URL into a string. Note that userinfo will not be included for security reasons.

# "http://pyjoyment.org"
Pyjo.URL.new().set(scheme='http', host='pyjoyment.org').to_str()

# "http://pyjoyment.org"
Pyjo.URL.new('http://daniel:s3cret@pyjoyment.org').to_str()
to_unsafe_str() → str[source]
string = url.to_unsafe_str()

Same as to_str(), but includes userinfo.

# "http://daniel:s3cret@pyjoyment.org"
Pyjo.URL.new('http://daniel:s3cret@pyjoyment.org').to_unsafe_str()
to_urllib() → SplitResult[source]
split = url.to_urllib()

Turn URL into a urllib.parse.SplitResult, for the standard library. Unlike to_str(), it includes userinfo, as the result holds all parts of the URL.

# "isabel"
Pyjo.URL.new('http://isabel:s3cret@pyjoyment.org/foo').to_urllib().username
userinfo: str | None
info = url.userinfo
url.userinfo = 'root:♥'

Userinfo part of this URL.

# "sri:t3st"
Pyjo.URL.new('https://sri:t3st@example.com/foo').userinfo
property username: str | None[source]
username = url.username

Username part of userinfo.

# "isabel"
Pyjo.URL.new('http://isabel:s3cret@pyjoyment.org').username