From 94988238de45910be84839657c4c572c612ca437 Mon Sep 17 00:00:00 2001 From: kratin tiwari <43218326+kratintiwari@users.noreply.github.com> Date: Mon, 24 May 2021 18:21:27 +0530 Subject: [PATCH] Create encdec.py --- encdec.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 encdec.py diff --git a/encdec.py b/encdec.py new file mode 100644 index 0000000..8763ef7 --- /dev/null +++ b/encdec.py @@ -0,0 +1,50 @@ +#! usr/bin/python3 + +import sys,getopt +import urllib.parse +import re + +def encdec(argv): + toEncode='' + toDecode='' + + try: + opts,args = getopt.getopt(argv,"h:e:d:",["enc=","dec="]) + except getopt.GetoptError: + print ('encdec.py -e -d ') + sys.exit(2) + + for opt,arg in opts: + if opt == '-h': + print ("""encdec.py -e '' -d ''""") + sys.exit() + + elif opt == '-e': + toEncode = str(arg) + + whitespace = re.search(' +', toEncode) #search if passed argument has whitespace or not + if whitespace: + + encodedString = urllib.parse.quote_plus(toEncode,safe='') + print('-------------------') + print(encodedString) + print('-------------------') + else: + + encodedString = urllib.parse.quote(toEncode,safe='') + print('-------------------') + print(encodedString) + print('-------------------') + + + elif opt == '-d': + toDecode = str(arg) + + decodedString = urllib.parse.unquote(toDecode) + print('-------------------') + print(decodedString) + print('-------------------') + + +if __name__ == "__main__": + encdec(sys.argv[1:])