It would be useful if urllib.request.urlopen
could be executed via the command line using python -m urllib.request
, in a similar way to how curl -f
works, to perform simple HTTP requests that fail on HTTP errors. For example, running:
python -m urllib.request http://localhost:8080/health
This functionality would be helpful in lightweight Docker containers, such as Python slim images, where curl
or other similar tools are not installed by default.
Proposal:
- Enable
urllib.request
to support direct command line execution for GET requests, similar to other-m
modules in Python. - Allow the return of a non-zero exit code if the request fails (e.g., for non-2xx HTTP responses), so that it can be easily integrated with health checks like in ECS or Docker Compose.
Use Case:
In containerized environments, specifically with Python slim images, it is a good practice to avoid installing extra tools to keep the image minimal and fast to build. A built-in python -m urllib.request
command would allow checking HTTP endpoints without additional installations.
Alternative:
It’s possible to run inline Python code:
python -c "import urllib.request; urllib.request.urlopen('http://localhost:8080/health')"
But it requires escaping quotes in some contexts like ECS task definitions (json), making it harder to maintain and understand.
"healthCheck": {
"command": [
"CMD-SHELL",
"python -c \"import urllib.request; urllib.request.urlopen('http://localhost:8080/health')\""
],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 10
}
Additional Information:
Installing curl
in a Python 3.12 slim image
FROM python:3.12-slim-bookworm
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
added approximately 5.13 MB, ie +4.3%. And it took 14s in my enviroment (poor bandwith).