Middle
Write Python code to check if a service is alive and what responses it gives.
sobes.tech AI
Answer from AI
python import requests
Service URL
url = 'http://example.com/health'
try: response = requests.get(url, timeout=5) if response.status_code == 200: print('Service is alive. Response:', response.text) else: print(f'Service responded with status code {response.status_code}') except requests.exceptions.RequestException as e: print('Error when trying to check the service:', e)
This script sends a GET request to the service's health endpoint (e.g., /health) and checks the response status. If the service is alive and responds with 200 OK, it prints the response body. Otherwise, it reports a problem.