Last active
December 31, 2016 08:07
-
-
Save photonxp/e97749b10a4508137ee156229b6107e3 to your computer and use it in GitHub Desktop.
simply decode base64 content from a string or a readable file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Demo code. Decode base64 content from a string or a readable file | |
# Works for Python 2.7.3, at least | |
# Usage | |
# decodebase64.py base64string | |
# decodebase64.py /absolute/path/to/base64/file | |
import sys | |
import os | |
import base64 | |
# testStr is for test usage, when they are assinged to the variable s to replace ''. | |
#testStr='Y29uZmlnLmpzb24ub2Jmc3Byb3h5LjIwMTYwMjIyXzE3NDcyOA==' | |
#testStr2='/path/to/base64/file' | |
def decodeStr(s): | |
rslt=base64.b64decode(s) | |
print 'Decoded result:\n',rslt | |
def decodeFile(fPath): | |
f = open(fPath, 'r') # better to use the with-as clause | |
readed = [line.rstrip() for line in f] | |
f.close() | |
strReaded = ''.join(readed) | |
print strReaded | |
decodeStr(strReaded) | |
def main(): | |
print 'Args: ',str(sys.argv) | |
# s = testStr | |
s = '' | |
if len(sys.argv) > 1: | |
s=sys.argv[1] | |
if os.path.isabs(s): | |
decodeFile(s) | |
elif len(s) > 0: | |
decodeStr(s) | |
else: | |
print "Nothing I can do" | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment