xmlrpc_test.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import xmlrpclib, httplib, sys
  2. # Usage: python xmlrpc_test.py command [params...]
  3. #
  4. # python script for sending an xmlrpc command to ser's xmlrpc module.
  5. # Note: it uses a re-defined transport class that does not depend on the
  6. # server side closing the connection (it can be used to send multiple
  7. # commands without closing the connections and it will work without any
  8. # special workarounds in the ser.cfg xmlrpc route).
  9. #
  10. # Credits: the transport class comes from ser_ctl, heavily trimmed to a very
  11. # basic version (the ser_ctl version is much more complex, supports
  12. # authentication, ssl a.s.o). See
  13. # http://git.sip-router.org/cgi-bin/gitweb.cgi?p=ser;a=blob;f=ser_ctl/serctl/serxmlrpc.py#l73 for the original (better) version.
  14. #
  15. #
  16. # History:
  17. # --------
  18. # 2009-07-13 initial version (andrei)
  19. #
  20. XMLRPC_SERVER = "127.0.0.1"
  21. XMLRPC_PORT = 5060
  22. class Transport:
  23. def __init__(self):
  24. self.conn=httplib.HTTPConnection(XMLRPC_SERVER, str(XMLRPC_PORT));
  25. def _http_request(self, uripath, body, host):
  26. self.conn.request("POST", uripath, body, {})
  27. def request(self, host, uripath, body, verbose=0):
  28. self._http_request(uripath, body, host)
  29. response=self.conn.getresponse()
  30. if response.status != 200:
  31. raise xmlrpclib.ProtocolError(host+uripath, response.status,
  32. response.reason, response.msg)
  33. data=response.read()
  34. parser, unmarshaller=xmlrpclib.getparser()
  35. parser.feed(data)
  36. parser.close()
  37. return unmarshaller.close()
  38. if len(sys.argv) < 2:
  39. sys.exit("Usage: "+sys.argv[0]+" rpc_command [args...]");
  40. transport=Transport()
  41. c=xmlrpclib.ServerProxy("http://" + XMLRPC_SERVER+ ":" + str(XMLRPC_PORT),
  42. transport)
  43. res=getattr(c, sys.argv[1])(*sys.argv[2:])
  44. print res;