Below is the code I came up with. It is a simple website with 2 pages. The first is for decoding base64 encoded strings and gives you the result as a file to download. The second is for encoding files and has a file upload form that gives you the resulting base64 encoded string as a result.
To run it, you need python and cherrypy installed (I'm using python 2.6.4 and cherrypy 3.2.2). Save the below code as
base64Decoder.py
and use the following command to run it:python base64Decoder.py
And then view it in your favourite browser at http://localhost:8064/
import base64 import cherrypy from cherrypy.lib.static import serve_file class base64Decoder(object): def index(self): return """ <html> <head><title>Base 64 Decoder</title></head> <body> <h2>Base 64 Decoder</h2> <a href="encoder">Switch to Encoder</a> <h2>Paste in the base 64 encoded string</h2> <form action="decode" method="post" enctype="multipart/form-data" target="_blank"> <textarea id="textToDecode" name="textToDecode" rows="5" cols="40"></textarea> Name of output file: <input type="text" id="fileName" name="fileName" /> <input type="submit" value="decode"/> </form> </body></html> """ index.exposed = True def encoder(self): return """ <html> <head><title>Base 64 Encoder</title></head> <body> <h2>Base 64 Encoder</h2> <a href="decoder">Switch to Decoder</a> <h2>Upload a file to encode</h2> <form action="encode" method="post" enctype="multipart/form-data" target="_blank"> filename: <input type="file" name="myFile" /> <input type="submit" value="encode"/> </form> </body></html> """ encoder.exposed = True def encode(self, myFile): cherrypy.response.headers['Content-Type'] = 'text/plain' data = myFile.file.read() return base64.encodestring(data) encode.exposed = True def decode(self, textToDecode, fileName): cherrypy.response.headers['Content-Type'] = 'application/octet-stream' dis = 'attachment; filename="%s"' % fileName cherrypy.response.headers['Content-Disposition'] = dis outstr = base64.decodestring(textToDecode) return outstr decode.exposed = True decoder = index cherrypy.server.socket_port = 8064 cherrypy.quickstart(base64Decoder(), '/')
No comments:
Post a Comment