DateTime strangeness when returning MySQLdb results thru xmlrpcserver

Ken Guest kwg at renre-europe.com
Fri Nov 23 09:43:04 EST 2001


OK, I've been writing an XML-RPC server in python to serve as an
intermediate between a client app and a mysql database - this works fine
for the most part in that I can open a database, execute sql and close
the database at will.
I'm using MySQLdb, xmlrpclib and xmlrpcserver to make all this work,
the version of python that I'm using is 2.0.1 and my xmlrpclib is at
version 0.9.9 with the last comment by Fredrik Lundh dated 2001-02-26.

And it works just fine until the resultset being returned references a 
datetime column - and keep getting an Internal Error (ie status code =
500).

I tweaked xmlrpcserver.py to return the error type and value [1] and
found that the reason is that xmlrpclib "cannot marshal <type
'DateTime'> objects". 
To be honest I'm not sure how the timestamps get converted to this type
as MySQLdb returns them as strings and examining xmlrpclib.py hasn't 
shed as much light on the problem as I'd like.

Anybody know what's going wrong here?


k.

[1] code block now looks like:
        except:
            # internal error, report as HTTP server error
            self.send_response(500)
            self.send_header("Content-type", "text/xml")
            self.send_header("X-Error-type", "%s" % (sys.exc_type))
            self.send_header("X-Error-value", "%s" % (sys.exc_value))
            self.end_headers()


[2]: this is the code for the pertinent method of the XMLRPCHandler
class that I'm using:

        def execute_sql(self, sql, database_id):
                """execute_sql(sql,database_id)"""
                global datadict
                return_this = []
                conn = datadict[database_id]
                curs = conn.cursor(MySQLdb.cursors.DictCursor)
                curs.execute(sql)
                print curs
		#prints
		#<MySQLdb.cursors.DictCursor instance at 0x819f144>
                return_this = curs.fetchall()
                curs.close()
                print return_this
		#prints:
		#({'s_datetimemapsynched': <DateTime object for '2001-08-10
12:01:37.00' at 81978b8>, 's_synched': 1L},)

		#this is odd as executing the same sql manually (well via MySQLdb) has
type reporting the timestamp as a string!

                #may need to iterate thruough results and cast them to
                #datatypes that the XML-RPC protocol understands..?
                return return_this 







More information about the Python-list mailing list