0
import base64
name = base64.b64decode('---boundary_177909_1c62465c-ca94-430f-95cb-04824165d0cf\r\nContent-Type: text/html; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n\r\nPCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBU\r\ncmFuc2l0aW9uYWwvL0VOIj4NCjxodG1sPg0KPGhlYWQ+DQo8bWV0YSBodHRw\r\nLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsgY2hh\r\ncnNldD11dGYtOCI+DQo8dGl0bGU+UmVzZXJ2YXRpb24gQ29uZmlybWF0aW9u\r\nICMxODQxNDY8L3RpdGxlPg0KPHN0eWxlIHR5cGU9InRleHQvY3NzIj4NClRE\r\nIHsNCmZvbnQtc2l6ZToxMnB4Ow0KZm9udC1mYW1pbHk6IFZlcmRhbmEsIEFy\r\naWFsLCBUYWhvbWEsIEhlbHZldGljYTsNCmNvbG9yOiMwMDAwMDA7DQp9DQph\r\nDQp7DQoJdGV4dC1kZWNvcmF0aW9uOiB1bmRlcmxpbmU7DQoJY29sb3I6IDMz\r\nZ2UnIC8+DQo8dGFibGUgYm9yZGVyPSIwIiBjZWxscGFkZGluZz0iMyIgY2Vs\r\nbHNwYWNpbmc9IjAiIHN0eWxlPSJib3JkZXItY29sbGFwc2U6IGNvbGxhcHNl\r\nIiBMTUgMDAwMDAgbg0KMDAwMDAyMzQ0NCAwMDAwMCBuDQowMDAwMDQwMzU2IDAw\r\nMDAwIG4NCnRyYWlsZXINCjw8DQovUm9vdCAxIDAgUg0KL0luZm8gOCAwIFIN\r\nCi9TaXplIDIwDQo+Pg0KDQpzdGFydHhyZWYNCjUwMTc1DQolJUVPRg0K\r\n\r\n----boundary_177910_c457e629-613b-4137-8d93-cf90400beda1--\r\n\r\n----boundary_177909_1c62465c-ca94-430f-95cb-04824165d0cf--\r\n\r\n\r\n')
print name 

From Python i am reading my gmail and getting encoded message like above. After that i am planing to decode that into normal HTML or normal Text. If i run above code then i am getting error like TypeError: Incorrect padding without modifying my string(which is enclosed with ( ) ) how to get the result.

Thanks, Ramesh.

El Bert
  • 2,958
  • 1
  • 28
  • 36

1 Answers1

0

You shouldn't be decoding the whole response you get because it appears to contain the boundaries of the request issued ---boundary_177909_.... All you need to decode is the body of the request for that you need to clean the input first:

import base64


data = '---boundary_177909_1c62465c-ca94-430f-95cb-04824165d0cf\r\nContent-Type: text/html; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n\r\nPCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBU\r\ncmFuc2l0aW9uYWwvL0VOIj4NCjxodG1sPg0KPGhlYWQ+DQo8bWV0YSBodHRw\r\nLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsgY2hh\r\ncnNldD11dGYtOCI+DQo8dGl0bGU+UmVzZXJ2YXRpb24gQ29uZmlybWF0aW9u\r\nICMxODQxNDY8L3RpdGxlPg0KPHN0eWxlIHR5cGU9InRleHQvY3NzIj4NClRE\r\nIHsNCmZvbnQtc2l6ZToxMnB4Ow0KZm9udC1mYW1pbHk6IFZlcmRhbmEsIEFy\r\naWFsLCBUYWhvbWEsIEhlbHZldGljYTsNCmNvbG9yOiMwMDAwMDA7DQp9DQph\r\nDQp7DQoJdGV4dC1kZWNvcmF0aW9uOiB1bmRlcmxpbmU7DQoJY29sb3I6IDMz\r\nZ2UnIC8+DQo8dGFibGUgYm9yZGVyPSIwIiBjZWxscGFkZGluZz0iMyIgY2Vs\r\nbHNwYWNpbmc9IjAiIHN0eWxlPSJib3JkZXItY29sbGFwc2U6IGNvbGxhcHNl\r\nIiBMTUgMDAwMDAgbg0KMDAwMDAyMzQ0NCAwMDAwMCBuDQowMDAwMDQwMzU2IDAw\r\nMDAwIG4NCnRyYWlsZXINCjw8DQovUm9vdCAxIDAgUg0KL0luZm8gOCAwIFIN\r\nCi9TaXplIDIwDQo+Pg0KDQpzdGFydHhyZWYNCjUwMTc1DQolJUVPRg0K\r\n\r\n----boundary_177910_c457e629-613b-4137-8d93-cf90400beda1--\r\n\r\n----boundary_177909_1c62465c-ca94-430f-95cb-04824165d0cf--\r\n\r\n\r\n'

# Remove all characters until the start of the actual data
# 1. find the index in the string at which the 'base64' encodeing appears
index = data.find("base64")
# 2. remove all characters up to "base64"
data = data[index + len("base64"):]

# Remove all characters after the boundaries
index = data.find("-")
data = data[:index]

# Remove all new line/caret return characters
data = data.replace("\r\n", "").replace("\r\\n", "")

# Now that the data is cleaned we can decode it but first it needs to be padded
# see https://en.wikipedia.org/wiki/Base64#Output_padding for more details
# and https://stackoverflow.com/a/49459036/2003420 for the implementation
print base64.b64decode(data + b'=' * (-len(data) % 4))
El Bert
  • 2,958
  • 1
  • 28
  • 36