Python Forum
decode base64 with python give error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decode base64 with python give error
#1
I have this code like this
            if str1 in content_email:
                print('test thử coi sao')
                aaa= '5paw44GX44GE54mp5Lu244GM55m76Yyy44GV44KM44G+44GX44Gf44Gu44Gn44CB44GU56K66KqN\r\n44GP44Gg44GV44GE44CCCQ0KCQ0K5Y+X5LuY5pel77yaMjAxOOW5tDA55pyIMjDml6UJDQrlvpfm\r\nhI/lhYjlkI3vvJrjg4/jgqbjgrnjg4bjgq/jg47moKrlvI/kvJrnpL4gLQkNCuaLheW9k+iAhe+8\r\nmiBpbWFuaXNoaQkNCueoruWIpTog5qeL6YCgQ0FE5YWl5YqbCQ0K54mp5Lu2IDEg5Lu2CQ0KDQoJ\r\n54mp5Lu244Kz44O844OJ77yaMTAwMDAwNTANCgnnianku7blkI3vvJrlpKfkuYXkv53kuK0xMeWP\r\ntw0KCeeZuuazqOWFg++8mumWouilv+ODm+ODvOODoA0KCeeZuuazqOWGheWuue+8muioguatow0K\r\nCeW4jOacm+e0jeacn++8mjIwMTgtMDktMjEgQU0NCg0K6YCj57Wh5qyE77yaDQo='

                data1 = base64.b64decode(aaa)
                print(data1)

                begin_encoding = content_email.find('Content-Transfer-Encoding: base64') + 41
                retun_begin = content_email[begin_encoding:-1]
                end_encoding = (retun_begin.find('Content-Type')) - 38
                retun_end = retun_begin[0:end_encoding]
                print('bắt đầu base64')
                print(retun_end)
                data = base64.b64decode(retun_end)
                print(data)
As you can see, the variable 'aaa' have the encode just like the value of variable 'retun_end'. But when I decode the 'aaa', it works perfectly, like this:

b'\xe6\x96\xb0\xe3\x81\x97\xe3\x81\x84\xe7\x89\xa9\xe4\xbb\xb6\xe3\x81\x8c\xe7\x99\xbb\xe9\x8c\xb2\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xbe\xe3\x81\x97\xe3\x81\x9f\xe3\x81\xae\xe3\x81\xa7\xe3\x80\x81\xe3\x81\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r\n'
Beside that, decode 'retun_end' gave me the wrong decode, it look like the same with decode 'aaa', but not
b'\xe6\x96\xb0\xe3\x81\x97\xe3\x81\x84\xe7\x89\xa9\xe4\xbb\xb6\xe3\x81\x8c\xe7\x99\xbb\xe9\x8c\xb2\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xbe\xe3\x81\x97\xe3\x81\x9f\xe3\x81\xae\xe3\x81\xa7\xe3\x80\x81\xe3\x81\x94\xe7\xa2\xba\xe8\xaa\x8d\xae~8\x18\xfe8\x1a\x0e8\x19^8\x18N8\x08 \x90\xd0\xa0\x90\xd0\xaeX\xf9~K\xb9\x8eiz^\xfb\xc9\xa3#\x03\x13\x8e[\x9bC\x03\x9ei\xc8\x83#\x0eizP\x90\xd0\xae[\xe9~j\xe7\x84\x8f\xe5\x85\x88\xe5\x90\x8d\xef\xbc\x9a\xe3\x83\x8f\xe3\x82\xa6\xe3\x82\xb9\xe3\x83\x86\xe3\x82\xaf\xe3\x83\x8e\xe6\xa0\xaa\xe5\xbc\x8f\xe4\xbc\x9a\xe7\xa4\xbe -\t\r\n\xe6\x8b\x85\xe5\xbd\x93\xe8\x80\x85\xef\xbc\xaey\xa2\x06\x96\xd6\x16\xe6\x976\x86\x90\x90\xd0\xaez\x8a\xeeX\x8aS\xa2\x0ejx\xbe\x98\n\x044\x14NXZ^X\xa9\xb0\x90\xd0\xaex\x9a\x9eK\xbbb\x03\x12\x0eK\xbb`\x90\xd0\xa0\xd0\xa0\x9a\xe7\xe7\x89\xa9\xe4\xbb\xb6\xe3\x82\xb3\xe3\x83\xbc\xe3\x83\x89\xef\xbc\x9a10000050\r\n\t\xe7\x89\xa9\xe4\xbb\xb6\xe5\x90\x8d\xef\xbc\x9a\xe5\xa4\xa7\xe4\xb9\x85\xe4\xbf\x9d\xe4\xb8\xad11\xe5\x8f\xae{p\xd0\xa0\x9ey\x9b\xaek:\x8eXX>\xfb\xc9\xae\x99j.\x8a[\xfe89\xbe8;\xce8:\x00\xd0\xa0\x9ey\x9b\xaek:\x8eXh^Z\xeb\x9e\xfb\xc9\xae\x8a\x88.j\xda0\xd0\xaa\xe7\t\xe5\xb8\x8c\xe6\x9c\x9b\xe7\xb4\x8d\xe6\x9c\x9f\xef\xbc\x9a2018-09-21 AM\r\n\r\n\xe9\x80\xa3\xe7\xb5\xa1\xe6\xac\x84\xef\xbc\x9a\r\n'
Anyone, who can explain why this can be possible? please help me
Reply
#2
Can someone help me please :((((
Reply
#3
I'd like to try, but the code you've shared cannot be used as is to try it out. Information is missing.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
Oke I will post all my code here, can you check it for me, please.
import imaplib
import base64
import chardet
import quopri
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
mail_st = "[email protected]"
pass_st = "password"
imap = imaplib.IMAP4_SSL("mail.eplatform.vn",993)
imap.login(mail_st,pass_st)
        #quét mail
imap.select("INBOX")

status, response = imap.search(None,'(UNSEEN)')
unread_msg_nums = response[0].split()

        #in tổng mail chưa đọc
print(len(unread_msg_nums))

        #bắt đầu đọc mail
for e_id in unread_msg_nums:

    _, response = imap.fetch(e_id, '(RFC822)')
    print('bắt đầu in mail')
    content_email = str(response[0][1])
    print (content_email)
    str1 = 'Content-Transfer-Encoding: base64'
    str2 = 'Content-Transfer-Encoding: quoted-printable'
    str3 = 'Content-Transfer-Encoding: 8bit'

            #chia câu lệnh if để chạy mail

    if str1 in content_email:
        print('test thử coi sao')
        aaa= '5paw44GX44GE54mp5Lu244GM55m76Yyy44GV44KM44G+44GX44Gf44Gu44Gn44CB44GU56K66KqN\r\n44GP44Gg44GV44GE44CCCQ0KCQ0K5Y+X5LuY5pel77yaMjAxOOW5tDA55pyIMjDml6UJDQrlvpfm\r\nhI/lhYjlkI3vvJrjg4/jgqbjgrnjg4bjgq/jg47moKrlvI/kvJrnpL4gLQkNCuaLheW9k+iAhe+8\r\nmiBpbWFuaXNoaQkNCueoruWIpTog5qeL6YCgQ0FE5YWl5YqbCQ0K54mp5Lu2IDEg5Lu2CQ0KDQoJ\r\n54mp5Lu244Kz44O844OJ77yaMTAwMDAwNTANCgnnianku7blkI3vvJrlpKfkuYXkv53kuK0xMeWP\r\ntw0KCeeZuuazqOWFg++8mumWouilv+ODm+ODvOODoA0KCeeZuuazqOWGheWuue+8muioguatow0K\r\nCeW4jOacm+e0jeacn++8mjIwMTgtMDktMjEgQU0NCg0K6YCj57Wh5qyE77yaDQo='
 
        data1 = base64.b64decode(aaa)
        print(data1)

        begin_encoding = content_email.find('Content-Transfer-Encoding: base64') + 41
        retun_begin = content_email[begin_encoding:-1]
        end_encoding = (retun_begin.find('Content-Type')) - 38
        retun_end = retun_begin[0:end_encoding]
        print('bắt đầu base64')
        print (type(retun_end))
        print(retun_end)
        if retun_end:
            data = base64.b64decode(retun_end)
            print(data)
You can change email and pw of your, and send some mail in japanese language to this mail. I suggest sending mail by Gmail, remember that my problem is about decode to Japanese language

You can get this content to test just like my code ( sending by Gmail to this mail in your code )
新しい物件が登録されましたので、ご確認ください。	
	
受付日:2018年09月20日	
得意先名:ハウステクノ株式会社 -	
担当者: imanishi	
種別: 構造CAD入力	
物件 1 件	

	物件コード:10000050
	物件名:大久保中11号
	発注元:関西ホーム
	発注内容:訂正
	希望納期:2018-09-21 AM

連絡欄:	
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decode string ? JohnnyCoffee 1 791 Jan-11-2023, 12:29 AM
Last Post: bowlofred
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,387 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,882 Jan-11-2021, 07:52 PM
Last Post: buran
  how to encode and decode same value absolut 2 2,280 Sep-08-2020, 09:46 AM
Last Post: TomToad
  python-resize-image unicode decode error Pedroski55 3 3,403 Apr-21-2020, 10:56 AM
Last Post: Pedroski55
  struct.decode() and '\0' deanhystad 1 3,144 Apr-09-2020, 04:13 PM
Last Post: TomToad
  download base64 email attachment cferguson 3 4,650 Feb-25-2020, 06:50 PM
Last Post: cferguson
  Getting decode error. shankar 8 10,281 Sep-20-2019, 10:05 AM
Last Post: tinman
  Post JSON from python to PHP don't give expected result pgagnebin 1 3,691 Sep-04-2019, 10:29 PM
Last Post: micseydel
  Base64 to retrieve message JonasH 0 1,626 Jul-22-2019, 04:00 PM
Last Post: JonasH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020