Use Python to Start/Shutdown a Host with cPanel API

cpanel, whm, hosting

The cPanel API is a set of web-based APIs that allow developers to interact with cPanel and WHM (Web Host Manager) through various programming languages such as PHP, Python, Ruby, etc. The API allows users to automate tasks, integrate cPanel with third-party software, and develop custom solutions for managing cPanel and WHM. The cPanel API uses XML-RPC, a remote procedure call (RPC) protocol that uses XML to encode its calls and HTTP as a transport mechanism. The API supports a wide range of functions, including creating email accounts, adding databases, managing DNS zones, and more, making it a powerful tool for managing cPanel and WHM.

Python code to start and shutdown a host using cPanel API:

import xmlrpc.client

# Set up the cPanel API client
server = xmlrpc.client.ServerProxy('https://yourdomain.com:2087') # Replace with your cPanel domain
username = 'your_username' # Replace with your cPanel username
password = 'your_password' # Replace with your cPanel password

# Start the host
result = server.cpsrvd.start_service(username, 'httpd') # Replace httpd with the service you want to start
if result['status'] == 1:
    print('Host started successfully')
else:
    print('Failed to start host:', result['statusmsg'])

# Shutdown the host
result = server.cpsrvd.stop_service(username, 'httpd') # Replace httpd with the service you want to stop
if result['status'] == 1:
    print('Host shutdown successfully')
else:
    print('Failed to shutdown host:', result['statusmsg'])

Note that you will need to replace yourdomain.com, your_username, and your_password with your actual cPanel domain, username, and password, respectively. You will also need to replace httpd with the service you want to start or stop (e.g., named, mysql, ftp, etc.).

See also  Formatting Python Code in IntelliJ - 5 minute read

Leave a Comment