On Mon, 7 Feb 2005 22:15:30 +0100, Lombardoni, Andrea <lombardo@inf.ethz.ch> wrote:
I would like to decode/encode data in base64 without resorting to an external Python method.
The following is in the Python interpreter, but I have verified that it works for Python scripts in Zope.
"Hello World!".encode('base64') 'SGVsbG8gV29ybGQh\n' 'SGVsbG8gV29ybGQh\n'.decode('base64') 'Hello World!' 'Clguba ebpxf!'.decode('rot13') u'Python rocks!'
I tried your suggestion, but it does not work in Zope.
I createad a simple Script(Python) object like this:
print 'ciao ciao'.encode('base64') return printed
But when I try to see it from Zope it returns this error:
Error Type: SystemError Error Value: module "encodings.base64" failed to register
Hmmm. I made a Script (Python) called decode with a parameter thing, and the code: return thing.decode('base64') and it works. I am using Zope-2.7.3, and the Test tab on the script I also made a more general one called base64, with parameters content and mode, and code: if mode == "encode": return content.encode('base64') elif mode == "decode": return content.decode('base64') else: raise "Unknown mode", mode This also works for me. You could do the whole thing as: return getattr(content, mode)(encoding) where encoding is also a form variable, but this seems a little unsafe to expose to the internet. What versions of Zope and Python do you have? I'm betting you are using Python-2.1, since it does not appear to have that encoding, whereas 2.2 and newer do. -- Computer interfaces should never be made of meat. Using GMail? Setting Reply-to address to <> disables this annoying feature.