A
download base64.py
Language: Python
LOC: 67
Project Info
NetMage
Server: SourceForge
Type: cvs
...ge\netmage\netmage\lib\Lib\
   .cvsignore
   __future__.py
   anydbm.py
   atexit.py
   base64.py
   BaseHTTPServer.py
   bdb.py
   binhex.py
   bisect.py
   calendar.py
   cgi.py
   CGIHTTPServer.py
   cmd.py
   code.py
   codecs.py
   colorsys.py
   commands.py
   compileall.py
   ConfigParser.py
   Cookie.py
   copy.py
   copy_reg.py
   dbexts.py
   difflib.py
   dircache.py
   doctest.py
   dospath.py
   dumbdbm.py
   fileinput.py
   fnmatch.py
   formatter.py
   fpformat.py
   ftplib.py
   getopt.py
   glob.py
   gopherlib.py
   gzip.py
   htmlentitydefs.py
   htmllib.py
   httplib.py
   imaplib.py
   imghdr.py
   isql.py
   javaos.py
   javapath.py
   jreload.py
   keyword.py
   linecache.py
   macpath.py
   macurl2path.py
   mailbox.py
   mailcap.py
   marshal.py
   mhlib.py
   mimetools.py
   mimetypes.py
   MimeWriter.py
   mimify.py
   multifile.py
   mutex.py
   nntplib.py
   ntpath.py
   nturl2path.py
   pdb.py
   pickle.py
   pipes.py
   popen2.py
   poplib.py
   posixfile.py
   posixpath.py
   pprint.py
   profile.py
   pstats.py
   pyclbr.py
   Queue.py
   quopri.py
   random.py
   re.py
   reconvert.py
   repr.py
   rfc822.py
   sched.py
   sgmllib.py
   shelve.py
   shutil.py
   SimpleHTTPServer.py
   site.py
   smtplib.py
   sndhdr.py
   socket.py
   SocketServer.py
   sre.py
   sre_compile.py
   sre_constants.py
   sre_parse.py
   stat.py
   string.py
   StringIO.py
   symbol.py
   telnetlib.py
   tempfile.py
   threading.py
   token.py
   tokenize.py
   traceback.py
   tzparse.py
   unittest.py
   urllib.py
   urlparse.py
   user.py
   UserDict.py
   UserList.py
   UserString.py
   warnings.py
   weakref.py
   whichdb.py
   whrandom.py
   xdrlib.py
   xmllib.py
   zipfile.py
   zlib.py

#! /usr/bin/env python

"""Conversions to/from base64 transport encoding as per RFC-1521."""

# Modified 04-Oct-95 by Jack to use binascii module

import binascii

__all__ = ["encode","decode","encodestring","decodestring"]

MAXLINESIZE = 76 # Excluding the CRLF
MAXBINSIZE = (MAXLINESIZE/4)*3

def encode(input, output):
    """Encode a file."""
    while 1:
        s = input.read(MAXBINSIZE)
        if not s: break
        while len(s) < MAXBINSIZE:
            ns = input.read(MAXBINSIZE-len(s))
            if not ns: break
            s = s + ns
        line = binascii.b2a_base64(s)
        output.write(line)

def decode(input, output):
    """Decode a file."""
    while 1:
        line = input.readline()
        if not line: break
        s = binascii.a2b_base64(line)
        output.write(s)

def encodestring(s):
    """Encode a string."""
    import StringIO
    f = StringIO.StringIO(s)
    g = StringIO.StringIO()
    encode(f, g)
    return g.getvalue()

def decodestring(s):
    """Decode a string."""
    import StringIO
    f = StringIO.StringIO(s)
    g = StringIO.StringIO()
    decode(f, g)
    return g.getvalue()

def test():
    """Small test program"""
    import sys, getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'deut')
    except getopt.error, msg:
        sys.stdout = sys.stderr
        print msg
        print """usage: %s [-d|-e|-u|-t] [file|-]
        -d, -u: decode
        -e: encode (default)
        -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
        sys.exit(2)
    func = encode
    for o, a in opts:
        if o == '-e': func = encode
        if o == '-d': func = decode
        if o == '-u': func = decode
        if o == '-t': test1(); return
    if args and args[0] != '-':
        func(open(args[0], 'rb'), sys.stdout)
    else:
        func(sys.stdin, sys.stdout)

def test1():
    s0 = "Aladdin:open sesame"
    s1 = encodestring(s0)
    s2 = decodestring(s1)
    print s0, `s1`, s2

if __name__ == '__main__':
    test()

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us