Python port of diractadmin API

nialloc

New member
Joined
Nov 5, 2009
Messages
4
# -*- coding: utf-8 -*-
import base64 # for encoding login details
import urllib # for encoding arguments sent to the server
import httplib2 # for communication with the direct admin server



class DirectAdmin:

# The init class takes a connection string in the following format
# "http://`user:password`your.directadmin.com:1234'"
# The string is split using the ` character user:password are base64 encoded and placed in the http header
# The remaining is rebuilt into the url: "http://your.directadmin.com:2222'"
def __init__(self, connectdata):
try:
self.connection_data = connectdata.split('`')
self.credentials = base64.b64encode(self.connection_data[1])
self.url = self.connection_data[0] + self.connection_data[2]
#print connectdata
except IndexError:
#print "Invalid connect string"

# The Retrieve method takes a series of dictionaries for the argument :
# {"command": "this is your direct admin command", "data":{"Parameter":"value", "Parameter2":"value" . . .} }
def Retrieve(self, argument):
h = httplib2.Http() # h refers to a new http object
h.follow_all_redirects = True # Follows all redirects

# headers is a dictionary with the encoded username and password (credentials) inserted at the end
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic %s' %self.credentials }

# The dictionary contained in the "data" key from "argument" is encoded into a GET string called "body"
body = urllib.urlencode(argument['data'])

# The url argument and encoded parameters in "body" make up the GET URL that interegates the directadmin server
uri = self.url +'/' + argument['command']+'?'+body

# The uri plus the GET/POST type and the headers are sent as a http request with the headers ( containing the login credentials )
# The response header is stored in resp and the response data stored in data
resp, data = h.request(uri, "GET", headers=headers)

# The data is parsed using the '&' character into a list
lines = data.split('&')

#print str(len(lines)) + '\n' + str(lines)
# If the right number of arguments are retrieved the response can be placed into a dictionary called content
if len(lines) % 2 == 0 :
content = "ERROR"
else:
content = dict((key, value) for key, value in (line.split('=') for line in lines))

# The response header and the response data ( now in a dictionary ) are returned
return {"header": resp, "content": content

# This is my own wrapper I send in the client ID and execute what ever commands I need here to grab
# as much or as little data as I need. I'm using this code in a Django application
def GetClientDetails(self, client_id):
#print str(self.url) + '\n' + str(self.credentials) + '\n'+ client_id + '\n\n'
user_config = self.Retrieve({"command":"CMD_API_SHOW_USER_CONFIG", "data":{"user":client_id}})

client_details = { "user_config": user_config }
return client_details
 
Last edited:
Acessing Diretcadmin using Python

This class will allow access to a direct admin server to execute any commands via GET


PHP:
# -*- coding: utf-8 -*-
import base64	# for encoding login details
import urllib	# for encoding arguments sent to the server
import httplib2  # for communication with the direct admin server



class DirectAdmin:

	# The init class takes a connection string in the following format
	# "http://`user:password`your.directadmin.com:1234'"
	# The string is split using the ` character user:password are base64 encoded and placed in the http header
	# The remaining is rebuilt into the url: "http://your.directadmin.com:1234'"
	def __init__(self, connectdata):
		try:
			self.connection_data = connectdata.split('`')
			self.credentials = base64.b64encode(self.connection_data[1])
			self.url = self.connection_data[0] + self.connection_data[2]
			#print connectdata
		except IndexError:
			#print "Invalid connect string"

	# The Retrieve method takes a series of dictionaries for the argument :
	# {"command": "this is your direct admin command", "data":{"Parameter":"value", "Parameter2":"value" . . .} }
	def Retrieve(self, argument):
		h = httplib2.Http() 		# h refers to a new http object
		h.follow_all_redirects = True   # Follows all redirects

		# headers is a dictionary with the encoded username and password (credentials) inserted at the end
		headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic %s' %self.credentials }

		# The dictionary contained in the "data" key from "argument" is encoded into a GET string called "body"
		body = urllib.urlencode(argument['data'])

		# The url argument and encoded parameters in "body" make up the GET URL that interegates the directadmin server
		uri  = self.url +'/' + argument['command']+'?'+body

		# The uri plus the GET/POST type and the headers are sent as a http request with the headers ( containing the login credentials )
		# The response header is stored in resp and the response data stored in data
		resp, data = h.request(uri, "GET", headers=headers)

		# The data is parsed using the '&' character into a list
		lines = data.split('&')

		#print str(len(lines)) + '\n' + str(lines)
		# If the right number of arguments are retrieved the response can be placed into a dictionary called content
		if len(lines) % 2 == 0 :
		    content = "ERROR"
		else:
		    content = dict((key, value) for key, value in (line.split('=') for line in lines))

		# The response header and the response data ( now in a dictionary ) are returned
		return {"header": resp, "content": content

	# This is my own wrapper I send in the client ID and execute what ever commands I need here to grab
	# as much or as little data as I need. I'm using this code in a Django application 
	def GetClientDetails(self, client_id):
		#print str(self.url) + '\n' + str(self.credentials) + '\n'+ client_id + '\n\n' 
		user_config = self.Retrieve({"command":"CMD_API_SHOW_USER_CONFIG", "data":{"user":client_id}})

		client_details = { "user_config": user_config }
		return client_details
 
Back
Top