urllib¶
urllib.request is in the standard library, so this example needs nothing
but jsonrpcclient. It sends the request as a JSON string and parses the bytes
that come back:
import logging
from urllib.request import Request, urlopen
from jsonrpcclient import Error, Ok, parse_json, request_json
http_request = Request(
"http://localhost:8000/",
data=request_json("ping").encode(),
headers={"Content-Type": "application/json"},
)
# urlopen raises urllib.error.HTTPError for a 4xx or 5xx status.
with urlopen(http_request, timeout=10) as http_response:
parsed = parse_json(http_response.read())
if isinstance(parsed, Ok):
print(parsed.result)
elif isinstance(parsed, Error):
logging.error(parsed.message)
urlopen raises urllib.error.HTTPError for an HTTP error status and
urllib.error.URLError if it can't connect. Both are OSError subclasses.
For a batch, send json.dumps([request(...), ...]) and handle the reply as
shown in Batches.