Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python xmlrpclib changes datetime object to DateTime instance on transmit

Messages exchanged between a client and a server using the xmlrpclib using Python 2.6.x creates a type of 'instance' on server side instead of type 'datetime'. On the client side I create a new

updateTime = datetime(year, month, day, hour, minute, second)
print type(updateTime)
print updateTime

results in

<type 'datetime.datetime'>
2015-10-07 10:21:52

when being send, the dictionary looks like this on the client side:

'updateTime': datetime.datetime(2015, 10, 7, 10, 21, 52)

but the incoming dictionary on the server side looks like this:

'updateTime': <DateTime '20151007T10:21:52' at 7f4dbf4ceb90>

printing the type and its string representation looks like this:

<type 'instance'>
20151007T10:21:52

We are using xmlrpclib.ServerProxy but changing use_datetime either to True or False does not make any difference at all.

xmlrpclib.ServerProxy('https://'+rpc_server_addr, allow_none=True, use_datetime=True)

Why is this happening? I expected a tpye 'datetime.datetime' on the receiving side as well.

like image 975
falc410 Avatar asked Oct 19 '22 00:10

falc410


2 Answers

You have to convert the xmlrpc.datetime format to a python datetime.datetime object.

The simplest way I have found to transform the object is:

import datetime

my_datetime = datetime.datetime.strptime(str(xmlrpc.datetime), '%Y%m%dT%H:%M:%S')
like image 108
Eric Rich Avatar answered Oct 21 '22 17:10

Eric Rich


The use_builtin_types=True works for me. All datetime values have type <class 'datetime.datetime'>.

Without this parameter all datetime were <class 'xmlrpc.client.DateTime'>

rpc = xmlrpc.client.ServerProxy('https://..../', use_builtin_types=True)

The Python3 XML-RPC client documentation says: The obsolete use_datetime flag is similar to use_builtin_types but it applies only to date/time values.

like image 31
dwich Avatar answered Oct 21 '22 15:10

dwich