Encoding and decoding

Next, let's look at the specific code for a couple of our APIs that we are going to use in this framework (encode, decode). We store the encode and decode functionalities in a separate Python file, called encodedecode.py and call them in the main.py file, which will be executed by Gunicorn. The code of the encodedecode.py file is as follows:

import base64  def encode(data):    encoded=base64.b64encode(str.encode(data))    if '[:]' in data:        text="Encoded string: "+encoded.decode('utf-8')        return text    else:        text="sample string format username[:]password"        return text    return encodeddef decode(data):    try:        decoded=base64.b64decode(data)        decoded=decoded.decode('utf-8')    except:        print("problem while decoding String") text="Error decoding ...

Get Practical Network Automation - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.