gws.lib.net¶
URL utilities and an HTTP client.
URLs:
parse_url splits a URL into a Url object (scheme, host, port, path, query parameters and so on),
make_url builds a URL string from such an object or a dict. make_qs, add_params,
extract_params and the quote_* functions handle query strings and URL quoting.
HTTP:
http_request sends a request with requests, using one session per process, so that
connections to the same host are reused. It does not raise on failures; instead, it returns an
HTTPResponse with ok set to False. Connection errors, timeouts and other request
errors get the pseudo status codes 900, 901 and 999. HTTPResponse.raise_if_failed turns a
failed response into an HTTPError subclass. The response text is decoded with the charset
from the content type header, or as UTF-8 or ISO-8859-1 if there is none.
Example:
u = gws.lib.net.parse_url('https://example.com/wms?SERVICE=WMS')
url = gws.lib.net.add_params(u.url, REQUEST='GetCapabilities')
res = gws.lib.net.http_request(url, timeout=10)
res.raise_if_failed()
xml = res.text
Source code: gws.lib.net
Package Contents¶
- gws.lib.net.add_params(url: str, params: dict = None, **kwargs) str¶
Add query parameters to a URL.
Existing parameters with the same names are replaced.
- Parameters:
url – A URL.
params – Parameters to add.
**kwargs – More parameters to add.
- Returns:
The new URL.
- gws.lib.net.extract_params(url: str) tuple[str, dict]¶
Split the query parameters off a URL.
- Parameters:
url – A URL.
- Returns:
The URL without the query string, and the query parameters.
- gws.lib.net.http_request(url, **kwargs) HTTPResponse¶
Send an HTTP request.
Failures are logged and returned as a response with
okset toFalse, they do not raise. By default, the connect and read timeouts are 60 seconds, certificates are verified withcertifi, and a GBD WebSuiteUser-Agentheader is sent.- Parameters:
url – Request URL.
**kwargs – Options for
requests.Session.request.methodis the HTTP method (GETby default),paramsare added to the URL, a numerictimeoutapplies to both connect and read.
- Returns:
The response.
- class gws.lib.net.HTTPResponse(ok: bool, url: str, res: requests.Response = None, text: str = None, status_code=0)¶
Response of
http_request.- ok¶
Trueif the request succeeded with a 2xx status.
- url¶
Request URL.
- content¶
Response body.
- content_type¶
Content type, without parameters.
- content_encoding¶
Charset from the content type header, or
None.
- status_code¶
HTTP status code, or a pseudo status code for requests that failed without a response.
- ok¶
- url¶
- raise_if_failed()¶
Raise an error if the request failed.
- Raises:
ConnectionError – If the connection failed.
Timeout – If the request timed out.
GenericError – If the request failed for another reason, without a response.
HTTPError – If the response status is not 2xx.
- gws.lib.net.is_abs_url(url)¶
Check if a URL has a host part, i.e. starts with
//or a scheme and//.- Parameters:
url – A URL.
- Returns:
A truthy match object if the URL is absolute,
Noneotherwise.
- gws.lib.net.make_qs(x) str¶
Convert a dict or a list of pairs to a query string.
Values are encoded as UTF-8 and quoted. Booleans become
trueandfalse, lists and other iterables are joined with commas, other values are converted withstr.- Parameters:
x – A dict, an object that can be converted to a dict, or a list of
(name, value)pairs.- Returns:
The query string, without
?.
- gws.lib.net.make_relative_url(path: str, params: dict = None, **kwargs) str¶
Build a URL from a path and query parameters, without a scheme and host.
- Parameters:
path – URL path; a leading slash is added if needed.
params – Query parameters.
**kwargs – More query parameters.
- Returns:
The URL.
- gws.lib.net.make_url(u: Url | dict | None = None, **kwargs) str¶
Build a URL string.
Uses
scheme,hostname,port,username,password,path,paramsandfragment; other keys, likequeryorurl, are ignored. The port is omitted if it is the default port of the scheme.- Parameters:
u – URL components, a Url object or a dict.
**kwargs – Components that override the values from
u.
- Returns:
The URL.
- gws.lib.net.parse_qs(x) dict¶
Parse a query string.
- Parameters:
x – Query string.
- Returns:
A dict that maps each parameter name to a list of its values.
- gws.lib.net.parse_url(url: str, **kwargs) Url¶
Parse a URL.
A URL without
//is treated as starting with a host name.- Parameters:
url – URL string.
**kwargs – Values to set on the
Urlobject after parsing.
- Returns:
A Url object.
- gws.lib.net.quote_param(s: str) str¶
Quote a string for use in a URL, including slashes.
- Parameters:
s – A string.
- Returns:
The quoted string.
- gws.lib.net.quote_path(s: str) str¶
Quote a URL path, leaving slashes as they are.
- Parameters:
s – A path.
- Returns:
The quoted path.
- gws.lib.net.unquote(s: str) str¶
Unquote a URL-quoted string.
- Parameters:
s – A quoted string.
- Returns:
The unquoted string.
- class gws.lib.net.Url(*args, **kwargs)¶
Bases:
gws.DataComponents of a URL, as returned by
parse_url.- params: dict¶
Query parameters. For repeated parameters, the first value is used.
- pathparts: gws.lib.osx.ParsePathResult¶
Components of the path.