How to Build a Simple Python Service Health Check Script
This guide shows how to write a Python script that constructs a service health URL from command‑line arguments, sends an HTTP request using the requests library, parses the JSON response, and raises an error if the service status is not "UP", providing clear success or failure feedback.
This article demonstrates a straightforward Python script for checking the health of a service via its /actuator/health endpoint.
import json
import sys
import requests
ip = sys.argv[1]
port = sys.argv[2]
service_name = sys.argv[3]
# Target URL
url = "http://" + ip + ":" + port + "/actuator/health"
print(url)
response = requests.get(url)
print(response.text)
data = json.loads(response.text)
health_status = data["status"]
if health_status != "UP":
# Raise a generic ValueError exception
raise ValueError("发布异常")
else:
print("服务:{} ip:{} 发布成功".format(service_name, ip))The script reads the IP address, port, and service name from command‑line arguments, builds the health‑check URL, prints it, performs a GET request, prints the raw response, parses the JSON to obtain the status field, and either raises an exception when the status is not UP or prints a success message.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
