How to use Python to execute a RPC

If you want to execute a RPC from an external host using Python, then you should use the following code.

import requests
import json

url='http://localhost/rpc.php'

session = requests.Session()
# Login and get the session cookie.
session.post(url, data=json.dumps({
	'service': 'session',
	'method': 'login',
	'params': {
		'username': 'admin',
		'password': 'xyz'
	}
}))
# Execute a RPC.
response = session.post(url, data=json.dumps({
	'service': 'system',
	'method': 'noop',
	'params': None
}))
print(response.json())
# Logout.
session.post(url, data=json.dumps({
	'service': 'session',
	'method': 'logout',
	'params': None
}))
# This RPC should fail because of an invalid session authentication.
response = session.post(url, data=json.dumps({
	'service': 'system',
	'method': 'noop',
	'params': None
}))
print(response.json())
session.close()

 

How to use curl to execute a RPC

If you want to execute a RPC from the command line on your host system, then you should use the following commands.

# Login
curl --request POST --cookie-jar cookies.txt --data '{"service":"session","method":"login","params":{"username":"admin","password":"<PASSWORD>"}}' <URL>/rpc.php
# Execute the RPC
curl --request POST --cookie cookies.txt --data '{"service":"<NAME>","method":"<NAME>","params":null}' <URL>/rpc.php
# Logout
curl --request POST --cookie cookies.txt --data '{"service":"session","method":"logout","params":null}' <URL>/rpc.php

Example:

curl --request POST --cookie cookies.txt --data '{"service":"system","method":"noop","params":null}' <URL>/rpc.php

New update available

The following changes were made:

openmediavault 1.11

  • Update locales.
  • Sort tabs in ‘Diagnostics | System Information | Status | Disk usage’.
  • Mantis 0001227: Show bridge and VLAN network interface devices within the GUI.
  • Mantis 0001246: Bonded Interface with 802.3ad-style link aggregation problem.
  • Mantis 0001247: Fix division by zero in OMVSystem::getCPUStats().