Issue
I try to port a python 2.x code to python 3. The line im struggeling with is
from mimetools import Message
...
headers = Message(StringIO(data.split('\r\n', 1)[1]))
i have figured out that mimetools are no longer present in python 3 and that the replacement is the email class. I tried out to replace it like this:
headers = email.message_from_file(io.StringIO(data.split('\r\n', 1)[1]))
but with that i get this error:
headers = email.message_from_file(io.StringIO(data.split('\r\n', 1)[1]))
TypeError: Type str doesn't support the buffer API
i am searching for an hint to do this porting from mimetools to email correct. The original code is not from me. It can be found here : https://gist.github.com/jkp/3136208
Solution
Alex's own solution from his comment:
import email
stream = io.StringIO()
rxString = data.decode("utf-8").split('\r\n', 1)[1]
stream.write(rxString)
headers = email.message_from_string(rxString)
Answered By - Alojz Janez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.