Redfish Python Library
The Redfish Python Library, developed by the DMTF, is a reference implementation enabling Python developers to communicate with Redfish-conformant APIs. It simplifies interactions by performing basic HTTP operations (GET, POST, PUT, PATCH, DELETE) on Redfish services. The library is actively maintained, currently at version 3.3.5, with new releases typically occurring every few months.
Warnings
- breaking Python 2.x support was dropped in version 3.0.0. Users on Python 2 must use `redfish<3.0.0`.
- gotcha The OpenStack 'python-redfish' module uses a conflicting package name. This library (DMTF's 'redfish') cannot coexist with it in the same environment.
- gotcha Before version 3.3.2, HTTP headers set on one `redfish_client` instance could unintentionally propagate to other instances due to a bug.
- gotcha In version 3.3.0, a workaround was added for Redfish services that incorrectly omit the 'Location' header during session login, which previously led to login failures or warnings. Services with this quirk may now work, but users might still see warnings if the workaround is triggered.
- gotcha The library switched its internal JSONPath implementation from `jsonpath_rw` to `jsonpath_ng` in version 3.3.5. While primarily an internal change, advanced users or those with custom logic deeply integrated with specific `jsonpath_rw` behaviors might observe subtle differences.
- gotcha Redfish sessions created with the library may persist on the server until they time out if `logout()` is not explicitly called. Although the client's destructor includes a logout, explicit calls are recommended for robust session management.
Install
-
pip install redfish
Imports
- redfish_client
import redfish client = redfish.redfish_client(...)
Quickstart
import redfish
import os
# Replace with your Redfish service details or use environment variables
LOGIN_HOST = os.environ.get('REDFISH_HOST', 'https://192.168.1.100')
LOGIN_ACCOUNT = os.environ.get('REDFISH_USERNAME', 'admin')
LOGIN_PASSWORD = os.environ.get('REDFISH_PASSWORD', 'password')
try:
# Create a Redfish object
REDFISH_OBJ = redfish.redfish_client(
base_url=LOGIN_HOST,
username=LOGIN_ACCOUNT,
password=LOGIN_PASSWORD,
default_prefix='/redfish/v1/'
)
# The client automatically logs in upon creation by default.
# You can explicitly call login() if check_connectivity=False was used during client creation.
# REDFISH_OBJ.login()
# Perform a GET operation on the Service Root
response = REDFISH_OBJ.get('/redfish/v1/')
if response.status == 200:
print("Successfully connected to Redfish Service Root:")
print(response.dict)
else:
print(f"Failed to connect to Redfish Service Root: {response.status} - {response.text}")
# Example: Get system information
systems_uri = response.dict.get('Systems', {}).get('@odata.id')
if systems_uri:
systems_response = REDFISH_OBJ.get(systems_uri)
if systems_response.status == 200:
print("\nSystems information:")
# Print the first system's summary if available
if systems_response.dict and 'Members' in systems_response.dict and len(systems_response.dict['Members']) > 0:
first_system_uri = systems_response.dict['Members'][0].get('@odata.id')
if first_system_uri:
first_system_response = REDFISH_OBJ.get(first_system_uri)
if first_system_response.status == 200:
print(first_system_response.dict)
else:
print(f"Failed to get first system details: {first_system_response.status}")
else:
print("No systems found.")
else:
print(f"Failed to get systems: {systems_response.status}")
else:
print("Systems collection URI not found in Service Root.")
except redfish.rest.v1.redfish_exception.ServerDownOrUnreachableError as e:
print(f"Error: Redfish service is down or unreachable: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
# Ensure to log out the session
if 'REDFISH_OBJ' in locals() and REDFISH_OBJ:
try:
REDFISH_OBJ.logout()
print("\nLogged out successfully.")
except Exception as e:
print(f"Error during logout: {e}")