kea_conn.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python
  2. # This file contains classes used for communication with Control Agent.
  3. # This class defines the HTTP request to be sent.
  4. # The supported parameters listed are:
  5. # - path (specifies the path on the server, CA uses only /)
  6. # - http_host - hostname of the CA
  7. # - http-port - TCP port of the CA
  8. # - command - specifies the command to send (e.g. list-commands)
  9. # - timeout - timeout (in ms)
  10. # - headers - extra HTTP headers may be added here
  11. # - version - version to be reported in HTTP header
  12. class CARequest:
  13. path = '/'
  14. http_host = ''
  15. http_port = 0
  16. command = ''
  17. timeout = 0
  18. params = ''
  19. headers = {}
  20. version = ""
  21. # Generates the content, out of specified command line
  22. # and optional content.
  23. # @todo: Add support for parameters
  24. # this stores the output in self.content
  25. def generateBody(self):
  26. self.content = '{ "command": "' + self.command + '"'
  27. if (len(self.params)):
  28. self.content += ', "parameters": { ' + self.params + ' }'
  29. self.content += ' }'
  30. # Generate HTTP headers
  31. #
  32. # In particular, this method generates Content-Length and its value.
  33. def generateHeaders(self):
  34. self.headers['Content-Type'] = 'application/json'
  35. self.headers['User-Agent'] = "Kea-shell/%s"%(self.version)
  36. self.headers['Accept'] = '*/*'
  37. self.headers['Content-Length'] = "%d"%(len(self.content))
  38. # This is a storage for generated command (input data to be sent over POST)
  39. content = ''
  40. # This class represents the HTTP response
  41. class CAResponse:
  42. # Constructor
  43. #
  44. # Three mandatory parameters are:
  45. # status - numerical number the describe the status (e.g. 200 = OK)
  46. # reason - textual explanation of what happened
  47. # body - the actual body structure of the response
  48. def __init__(self, status, reason, body):
  49. self.status = status
  50. self.reason = reason
  51. self.body = body
  52. status = 0
  53. reason = ''
  54. body = ''
  55. # Used for debugging
  56. #
  57. # if defug is true, this prints even more information
  58. def printResp(self, debug = False):
  59. if (debug):
  60. print(self.status)
  61. print(self.reason)
  62. print(self.body)