NetUtil.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from httplib import HTTPS
  16. from urlparse import urlparse
  17. import time
  18. import logging
  19. import pprint
  20. import traceback
  21. import httplib
  22. logger = logging.getLogger()
  23. class NetUtil:
  24. CONNECT_SERVER_RETRY_INTERVAL_SEC = 10
  25. HEARTBEAT_IDDLE_INTERVAL_SEC = 10
  26. HEARTBEAT_NOT_IDDLE_INTERVAL_SEC = 5
  27. # Url within server to request during status check. This url
  28. # should return HTTP code 200
  29. SERVER_STATUS_REQUEST = "{0}/cert/ca"
  30. # For testing purposes
  31. DEBUG_STOP_RETRIES_FLAG = False
  32. def checkURL(self, url):
  33. """Try to connect to a given url. Result is True if url returns HTTP code 200, in any other case
  34. (like unreachable server or wrong HTTP code) result will be False
  35. """
  36. logger.info("DEBUG:: Connecting to the following url " + url);
  37. try:
  38. parsedurl = urlparse(url)
  39. ca_connection = httplib.HTTPSConnection(parsedurl[1])
  40. ca_connection.request("GET", parsedurl[2])
  41. response = ca_connection.getresponse()
  42. status = response.status
  43. logger.info("DEBUG: Calling url received " + str(status))
  44. if status == 200:
  45. return True
  46. else:
  47. return False
  48. except Exception, e:
  49. logger.info("Failed to connect to " + str(url) + " due to " + str(e))
  50. return False
  51. def try_to_connect(self, server_url, max_retries, logger = None):
  52. """Try to connect to a given url, sleeping for CONNECT_SERVER_RETRY_INTERVAL_SEC seconds
  53. between retries. No more than max_retries is performed. If max_retries is -1, connection
  54. attempts will be repeated forever until server is not reachable
  55. Returns count of retries
  56. """
  57. if logger is not None:
  58. logger.info("DEBUG: Trying to connect to the server at " + server_url)
  59. retries = 0
  60. while (max_retries == -1 or retries < max_retries) and not self.DEBUG_STOP_RETRIES_FLAG:
  61. server_is_up = self.checkURL(self.SERVER_STATUS_REQUEST.format(server_url))
  62. if server_is_up:
  63. break
  64. else:
  65. if logger is not None:
  66. logger.info('Server at {0} is not reachable, sleeping for {1} seconds...'.format(server_url,
  67. self.CONNECT_SERVER_RETRY_INTERVAL_SEC))
  68. retries += 1
  69. time.sleep(self.CONNECT_SERVER_RETRY_INTERVAL_SEC)
  70. return retries