Search code examples
python-3.xxml-rpc

Sending XMLRPC Request with python3


i want to make a request with python and xmlrpc.

from xmlrpc.client import ServerProxy, datetime
import ssl
import hashlib

hash_object = hashlib.md5(b'USER*PASSWORD')

test = ServerProxy('https://IP/xml-rpc?de.vertico.starface.auth=%s' % hash_object.hexdigest(),
                             verbose=False, use_datetime=True, 
                             context=ssl._create_unverified_context())
print(test.Queue.getHistoryData({"queueName" : "Hauptgruppe","from" : "20161230T12:59:05", "to" : "20170701T12:59:05"}))

The body needs to be like this:

<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
   <methodName>Queue.getHistoryData</methodName>
   <params>
      <param>
         <value>
            <struct>
               <member>
                  <name>queueName</name>
                  <value>
                     <string>testIq</string>
                  </value>
               </member>
               <member>
                  <name>from</name>
                  <value>
                     <string>20150701T12:59:05</string>
                  </value>
               </member>
               <member>
                  <name>to</name>
                  <value>
                     <string>20160701T12:59:05</string>
                  </value>
               </member>
            </struct>
         </value>
      </param>
   </params>
</methodCall>

But i keep getting following error as result. And i dont know how to fix it. Can someone please give me some help?

xmlrpc.client.Fault: <Fault 1: 'java.lang.ClassCastException : java.lang.String
cannot be cast to java.util.Date'>

Solution

  • Given the error message, I suspect that you need to send <dateTime.iso8601> tags instead. Try sending datetime objects:

    import datetime
    
    test = ServerProxy(
        'https://IP/xml-rpc?de.vertico.starface.auth=%s' % hash_object.hexdigest(),
        verbose=False, use_builtin_types=True, 
        context=ssl._create_unverified_context())
    print(test.Queue.getHistoryData({
        "queueName": "Hauptgruppe",
        "from" : datetime.datetime(2016, 12, 30, 12, 59, 5),
        "to": datetime.datetime(2017, 7, 1, 12, 59, 5)}))
    

    Note that I switched to using use_builtin_types as use_datetime is deprecated.