xmlrpc.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #Licensed to the Apache Software Foundation (ASF) under one
  2. #or more contributor license agreements. See the NOTICE file
  3. #distributed with this work for additional information
  4. #regarding copyright ownership. The ASF licenses this file
  5. #to you under the Apache License, Version 2.0 (the
  6. #"License"); you may not use this file except in compliance
  7. #with the License. You may obtain a copy of the License at
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. import xmlrpclib, time, random, signal
  15. from hodlib.Common.util import hodInterrupt
  16. class hodXRClient(xmlrpclib.ServerProxy):
  17. def __init__(self, uri, transport=None, encoding=None, verbose=0,
  18. allow_none=0, installSignalHandlers=1, retryRequests=True, timeOut=15):
  19. xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, verbose,
  20. allow_none)
  21. self.__retryRequests = retryRequests
  22. self.__timeOut = timeOut
  23. if (installSignalHandlers!=0):
  24. self.__set_alarm()
  25. def __set_alarm(self):
  26. def alarm_handler(sigNum, sigHandler):
  27. raise Exception("XML-RPC socket timeout.")
  28. signal.signal(signal.SIGALRM, alarm_handler)
  29. def __request(self, methodname, params):
  30. response = None
  31. retryWaitTime = 5 + random.randint(0, 5)
  32. for i in range(0, 30):
  33. signal.alarm(self.__timeOut)
  34. try:
  35. response = self._ServerProxy__request(methodname, params)
  36. signal.alarm(0)
  37. break
  38. except Exception:
  39. if self.__retryRequests:
  40. if hodInterrupt.isSet():
  41. raise HodInterruptException()
  42. time.sleep(retryWaitTime)
  43. else:
  44. raise Exception("hodXRClientTimeout")
  45. return response
  46. def __getattr__(self, name):
  47. # magic method dispatcher
  48. return xmlrpclib._Method(self.__request, name)