URL¶
The rite.net.url submodule provides helpers for parsing, building, and encoding URLs.
URL Module¶
URL parsing and manipulation utilities.
This submodule provides utilities for parsing, building, encoding, and decoding URLs.
Examples¶
from rite.net.url import ( ... url_parse, ... url_encode, ... url_decode ... ) url_encode("hello world") 'hello%20world'
Modules¶
url_build
¶
URL Builder¶
Build URL from components.
Examples¶
from rite.net.url import url_build url_build("https", "example.com", "/path", query="q=1") 'https://example.com/path?q=1'
Functions¶
url_build(scheme: str = '', netloc: str = '', path: str = '', params: str = '', query: str | dict[str, str] = '', fragment: str = '') -> str
¶
Build URL from components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scheme
|
str
|
URL scheme (http, https, etc.). |
''
|
netloc
|
str
|
Network location (domain:port). |
''
|
path
|
str
|
Path component. |
''
|
params
|
str
|
Parameters (rarely used). |
''
|
query
|
str | dict[str, str]
|
Query string or dict. |
''
|
fragment
|
str
|
Fragment identifier. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
Complete URL string. |
Examples:
>>> url_build("https", "example.com", "/api")
'https://example.com/api'
>>> url_build("http", "test.com", query={"a": "1", "b": "2"})
'http://test.com?a=1&b=2'
Notes
Uses urllib.parse.urlunparse. Query can be string or dict.
url_decode
¶
url_encode
¶
URL Encoder¶
Encode URL component.
Examples¶
from rite.net.url import url_encode url_encode("hello world") 'hello%20world'
Functions¶
url_encode(text: str, safe: str = '') -> str
¶
URL-encode text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to encode. |
required |
safe
|
str
|
Characters not to encode. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
URL-encoded text. |
Examples:
Notes
Uses urllib.parse.quote. Encodes special characters to %XX format.
url_parse
¶
URL Parser¶
Parse URL into components.
Examples¶
from rite.net.url import url_parse url_parse("https://example.com:8080/path?q=1#frag") # doctest: +SKIP
Functions¶
url_parse(url: str) -> ParseResult
¶
Parse URL into components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL string to parse. |
required |
Returns:
| Type | Description |
|---|---|
ParseResult
|
ParseResult with scheme, netloc, path, params, query, fragment. |
Examples:
>>> result = url_parse("http://example.com/path")
>>> result.scheme
'http'
>>> result.netloc
'example.com'
Notes
Uses urllib.parse.urlparse. Returns ParseResult named tuple.
url_parse_query
¶
Query String Parser¶
Parse query string to dictionary.
Examples¶
from rite.net.url import url_parse_query url_parse_query("a=1&b=2&c=3")
Functions¶
url_parse_query(query: str) -> dict[str, list[str]]
¶
Parse query string to dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Query string (without ?). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Dictionary with lists of values. |
Examples:
>>> url_parse_query("key=value")
{'key': ['value']}
>>> url_parse_query("a=1&a=2&b=3")
{'a': ['1', '2'], 'b': ['3']}
Notes
Uses urllib.parse.parse_qs. Values are always lists (multiple values supported).
options: show_root_heading: true show_source: false heading_level: 2