base64.decodestring or xmlrpc (in zope) is truncating strings

Fredrik Lundh fredrik at
Wed Feb 21 15:45:01 EST 2001

Craig Dunigan wrote:
>   meta_type = fileObject.meta_type
>   if meta_type == 'DTML Document':
>     return ("OK", base64.encodestring(fileObject.document_src()))
>   elif meta_type == 'File':
>     return ("OK", base64.encodestring(
>   elif meta_type == 'Image':
>     return ("OK", base64.encodestring(
>   else:
>     return ("FAIL", 'Unsupported meta_type')

xmlrpc usage note: instead of encoding and decoding yourself,
you can use the xmlrpclib.Binary wrapper:

    if meta_type == 'DTML Document':
        return ("OK", xmlrpclib.Binary(fileObject.document_src())
    elif meta_type in ('File', 'Image'):
        return ("OK", xmlrpclib.Binary(
        return ("FAIL", 'Unsupported meta_type')

the receiver gets a Binary object as well, and can pick out
the original string from the data attribute:

    if retval[0] == "OK":
        return retval[1].data

> the calling object of "getFile" does a write to the local filesystem

...which usually means that you forgot to open the file in
binary mode:

    file = open(filename, "w") # open in text mode
    file = open(filename, "wb") # open in binary mode

Cheers /F

<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list