Skip to content

Instantly share code, notes, and snippets.

@ilya-muhortov
Created March 10, 2020 08:10
Show Gist options
  • Save ilya-muhortov/5a32d639afa20b5c89831baed9ec0532 to your computer and use it in GitHub Desktop.
Save ilya-muhortov/5a32d639afa20b5c89831baed9ec0532 to your computer and use it in GitHub Desktop.
import re,base64, quopri
def encoded_words_to_text(encoded_words):
encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}='
charset, encoding, encoded_text = re.match(encoded_word_regex, encoded_words).groups()
if encoding is 'B':
byte_string = base64.b64decode(encoded_text)
elif encoding is 'Q':
byte_string = quopri.decodestring(encoded_text)
else:
raise ValueError
return byte_string.decode(charset)
print(encoded_words_to_text('=?UTF-8?B?0LLQtdGI0LjQstCw0L3QuNC1LlhMUw==?='))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment