kea_connector3.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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 is PYTHON 3.x version of HTTP connection establishment
  8. """
  9. import urllib.request
  10. from kea_conn import CAResponse # CARequest
  11. def send_to_control_agent(params):
  12. """ Sends a request to Control Agent, receives a response and returns it."""
  13. # First, create the URL
  14. url = "http://" + params.http_host + ":"
  15. url += str(params.http_port) + str(params.path)
  16. # Now preprare the request (URL, headers and body)
  17. req = urllib.request.Request(url=url,
  18. data=str.encode(params.content),
  19. headers=params.headers)
  20. # Establish connection, send the request.
  21. resp = urllib.request.urlopen(req)
  22. # Now get the response details, put it in CAResponse and return it
  23. result = CAResponse(resp.getcode(), resp.reason,
  24. resp.read().decode("utf-8"))
  25. return result