xmlrpc.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. class hodXRClient(xmlrpclib.ServerProxy):
  16. def __init__(self, uri, transport=None, encoding=None, verbose=0,
  17. allow_none=0, installSignalHandlers=1, retryRequests=True, timeOut=15):
  18. xmlrpclib.ServerProxy.__init__(self, uri, transport, encoding, verbose,
  19. allow_none)
  20. self.__retryRequests = retryRequests
  21. self.__timeOut = timeOut
  22. if (installSignalHandlers!=0):
  23. self.__set_alarm()
  24. def __set_alarm(self):
  25. def alarm_handler(sigNum, sigHandler):
  26. raise Exception("XML-RPC socket timeout.")
  27. signal.signal(signal.SIGALRM, alarm_handler)
  28. def __request(self, methodname, params):
  29. response = None
  30. retryWaitTime = 5 + random.randint(0, 5)
  31. for i in range(0, 30):
  32. signal.alarm(self.__timeOut)
  33. try:
  34. response = self._ServerProxy__request(methodname, params)
  35. signal.alarm(0)
  36. break
  37. except Exception:
  38. if self.__retryRequests:
  39. time.sleep(retryWaitTime)
  40. else:
  41. raise Exception("hodXRClientTimeout")
  42. return response
  43. def __getattr__(self, name):
  44. # magic method dispatcher
  45. return xmlrpclib._Method(self.__request, name)