kea_conn.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. """
  7. This file contains classes used for communication with Control Agent.
  8. """
  9. class CARequest:
  10. """
  11. This class defines the HTTP request to be sent.
  12. The supported parameters listed are:
  13. - path (specifies the path on the server, CA uses only /)
  14. - http_host - hostname of the CA
  15. - http-port - TCP port of the CA
  16. - command - specifies the command to send (e.g. list-commands)
  17. - service - specifies service that is target for the command (e.g. dhcp4)
  18. - timeout - timeout (in ms)
  19. - args - extra arguments my be added here
  20. - headers - extra HTTP headers may be added here
  21. - version - version to be reported in HTTP header
  22. """
  23. path = '/'
  24. http_host = ''
  25. http_port = 0
  26. command = ''
  27. service = ''
  28. timeout = 0
  29. args = ''
  30. headers = {}
  31. version = ""
  32. # This is a storage for generated command (input data to be sent over POST)
  33. content = ''
  34. def generate_body(self):
  35. """
  36. Generates the content, out of specified command line
  37. and optional content.
  38. this stores the output in self.content
  39. """
  40. self.content = '{ "command": "' + self.command + '"'
  41. if self.service is not None:
  42. self.service = [x for x in self.service if x]
  43. if len(self.service) > 0:
  44. self.content += ', "service": ["' + '","'.join(self.service) + '"]'
  45. if len(self.args) > 1:
  46. self.content += ', "arguments": { ' + self.args + ' }'
  47. self.content += ' }'
  48. def generate_headers(self):
  49. """
  50. Generate HTTP headers
  51. In particular, this method generates Content-Length and its value.
  52. """
  53. self.headers['Content-Type'] = 'application/json'
  54. self.headers['User-Agent'] = "Kea-shell/%s"%(self.version)
  55. self.headers['Accept'] = '*/*'
  56. self.headers['Content-Length'] = "%d"%(len(self.content))
  57. class CAResponse:
  58. """
  59. This class represents the HTTP response
  60. """
  61. def __init__(self, status, reason, body):
  62. """
  63. Constructor
  64. Three mandatory parameters are:
  65. status - numerical number the describe the status (e.g. 200 = OK)
  66. reason - textual explanation of what happened
  67. body - the actual body structure of the response
  68. """
  69. self.status = status
  70. self.reason = reason
  71. self.body = body
  72. status = 0
  73. reason = ''
  74. body = ''
  75. def print_response(self, debug=False):
  76. """
  77. Used for debugging
  78. if debug is true, this prints even more information
  79. """
  80. if debug:
  81. print(self.status)
  82. print(self.reason)
  83. print(self.body)