|
@@ -1,17 +1,25 @@
|
|
-#!/usr/bin/python
|
|
|
|
|
|
+# Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
+#
|
|
|
|
+# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
+# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
-# This file contains classes used for communication with Control Agent.
|
|
|
|
|
|
+"""
|
|
|
|
+This file contains classes used for communication with Control Agent.
|
|
|
|
+"""
|
|
|
|
|
|
-# This class defines the HTTP request to be sent.
|
|
|
|
-# The supported parameters listed are:
|
|
|
|
-# - path (specifies the path on the server, CA uses only /)
|
|
|
|
-# - http_host - hostname of the CA
|
|
|
|
-# - http-port - TCP port of the CA
|
|
|
|
-# - command - specifies the command to send (e.g. list-commands)
|
|
|
|
-# - timeout - timeout (in ms)
|
|
|
|
-# - headers - extra HTTP headers may be added here
|
|
|
|
-# - version - version to be reported in HTTP header
|
|
|
|
class CARequest:
|
|
class CARequest:
|
|
|
|
+ """
|
|
|
|
+ This class defines the HTTP request to be sent.
|
|
|
|
+ The supported parameters listed are:
|
|
|
|
+ - path (specifies the path on the server, CA uses only /)
|
|
|
|
+ - http_host - hostname of the CA
|
|
|
|
+ - http-port - TCP port of the CA
|
|
|
|
+ - command - specifies the command to send (e.g. list-commands)
|
|
|
|
+ - timeout - timeout (in ms)
|
|
|
|
+ - headers - extra HTTP headers may be added here
|
|
|
|
+ - version - version to be reported in HTTP header
|
|
|
|
+ """
|
|
path = '/'
|
|
path = '/'
|
|
http_host = ''
|
|
http_host = ''
|
|
http_port = 0
|
|
http_port = 0
|
|
@@ -20,39 +28,47 @@ class CARequest:
|
|
params = ''
|
|
params = ''
|
|
headers = {}
|
|
headers = {}
|
|
version = ""
|
|
version = ""
|
|
|
|
+ # This is a storage for generated command (input data to be sent over POST)
|
|
|
|
+ content = ''
|
|
|
|
|
|
- # Generates the content, out of specified command line
|
|
|
|
- # and optional content.
|
|
|
|
- # @todo: Add support for parameters
|
|
|
|
- # this stores the output in self.content
|
|
|
|
- def generateBody(self):
|
|
|
|
|
|
+ def generate_body(self):
|
|
|
|
+ """
|
|
|
|
+ Generates the content, out of specified command line
|
|
|
|
+ and optional content.
|
|
|
|
+ @todo: Add support for parameters
|
|
|
|
+ this stores the output in self.content
|
|
|
|
+ """
|
|
self.content = '{ "command": "' + self.command + '"'
|
|
self.content = '{ "command": "' + self.command + '"'
|
|
- if (len(self.params)):
|
|
|
|
|
|
+ if len(self.params):
|
|
self.content += ', "parameters": { ' + self.params + ' }'
|
|
self.content += ', "parameters": { ' + self.params + ' }'
|
|
self.content += ' }'
|
|
self.content += ' }'
|
|
|
|
|
|
- # Generate HTTP headers
|
|
|
|
- #
|
|
|
|
- # In particular, this method generates Content-Length and its value.
|
|
|
|
- def generateHeaders(self):
|
|
|
|
|
|
+ def generate_headers(self):
|
|
|
|
+ """
|
|
|
|
+ Generate HTTP headers
|
|
|
|
+
|
|
|
|
+ In particular, this method generates Content-Length and its value.
|
|
|
|
+ """
|
|
self.headers['Content-Type'] = 'application/json'
|
|
self.headers['Content-Type'] = 'application/json'
|
|
self.headers['User-Agent'] = "Kea-shell/%s"%(self.version)
|
|
self.headers['User-Agent'] = "Kea-shell/%s"%(self.version)
|
|
self.headers['Accept'] = '*/*'
|
|
self.headers['Accept'] = '*/*'
|
|
self.headers['Content-Length'] = "%d"%(len(self.content))
|
|
self.headers['Content-Length'] = "%d"%(len(self.content))
|
|
|
|
|
|
- # This is a storage for generated command (input data to be sent over POST)
|
|
|
|
- content = ''
|
|
|
|
|
|
|
|
-# This class represents the HTTP response
|
|
|
|
class CAResponse:
|
|
class CAResponse:
|
|
|
|
+ """
|
|
|
|
+ This class represents the HTTP response
|
|
|
|
+ """
|
|
|
|
|
|
- # Constructor
|
|
|
|
- #
|
|
|
|
- # Three mandatory parameters are:
|
|
|
|
- # status - numerical number the describe the status (e.g. 200 = OK)
|
|
|
|
- # reason - textual explanation of what happened
|
|
|
|
- # body - the actual body structure of the response
|
|
|
|
def __init__(self, status, reason, body):
|
|
def __init__(self, status, reason, body):
|
|
|
|
+ """
|
|
|
|
+ Constructor
|
|
|
|
+
|
|
|
|
+ Three mandatory parameters are:
|
|
|
|
+ status - numerical number the describe the status (e.g. 200 = OK)
|
|
|
|
+ reason - textual explanation of what happened
|
|
|
|
+ body - the actual body structure of the response
|
|
|
|
+ """
|
|
self.status = status
|
|
self.status = status
|
|
self.reason = reason
|
|
self.reason = reason
|
|
self.body = body
|
|
self.body = body
|
|
@@ -61,11 +77,13 @@ class CAResponse:
|
|
reason = ''
|
|
reason = ''
|
|
body = ''
|
|
body = ''
|
|
|
|
|
|
- # Used for debugging
|
|
|
|
- #
|
|
|
|
- # if defug is true, this prints even more information
|
|
|
|
- def printResp(self, debug = False):
|
|
|
|
- if (debug):
|
|
|
|
|
|
+ def print_response(self, debug=False):
|
|
|
|
+ """
|
|
|
|
+ Used for debugging
|
|
|
|
+
|
|
|
|
+ if debug is true, this prints even more information
|
|
|
|
+ """
|
|
|
|
+ if debug:
|
|
print(self.status)
|
|
print(self.status)
|
|
print(self.reason)
|
|
print(self.reason)
|
|
print(self.body)
|
|
print(self.body)
|