Найти - Пользователи
Полная версия: Метод pop3.retr и запись в файл
Начало » Центр помощи » Метод pop3.retr и запись в файл
1
PythonRecrut
Здравствуйте, столкнулся с проблемой записи eml в файл, при использовании библиотеки pop3.
import poplib                   
                                
username = 'MyMail@mail.ru'
password = 'MyPass'            
p = poplib.POP3('pop.mail.ru')  
p.user(username)                
p.pass_(password)               
numMessages = len(p.list()[1])                  
for i in range(numMessages):                    
    for j in p.retr(i+1)[1]:                    
        outf = open('eml/%s.eml' % i, "a")      
        outf.write('\n\r'.join(p.retr(i+1)[1])) 
        outf.close()                            
p.quit()  

Выдает ошибку
outf.write('\n\r'.join(p.retr(i+1)))
TypeError: sequence item 0: expected str instance, bytes found

Пытался в режиме записи двоичных данных “ab”, то в одну строчку все пишет, то опять ошибки.
print(j) выводит eml в консоль, а мне нужно сохранить почту файлами у себя.
Помогите пожалуйста.
Спасибо.

py.user.next
PythonRecrut
print(j) выводит eml в консоль
у print() есть аргумент file

>>> import sys
>>> 
>>> print('test', file=sys.stderr)
test
>>>

PythonRecrut
TypeError: sequence item 0: expected str instance, bytes found

>>> ' '.join((b'abc', b'def'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, bytes found
>>> 
>>> b' '.join((b'abc', b'def'))
b'abc def'
>>>

PythonRecrut
'\n\r'.
\r\n (0d 0a)

PythonRecrut
Спасибо за помощь.

В итоге получилось.
numMessages = len(p.list()[1])
for i in range(numMessages):
    outf = open('eml%s.eml' % i, 'wb')
    outf.write(b'\n'.join(p.retr(i + 1)[1]))
    outf.close()
py.user.next
там должно быть CRLF в любой системе в соответствии с rfc5322

   Messages are divided into lines of characters.  A line is a series of
characters that is delimited with the two characters carriage-return
and line-feed; that is, the carriage return (CR) character (ASCII
value 13) followed immediately by the line feed (LF) character (ASCII
value 10). (The carriage return/line feed pair is usually written in
this document as "CRLF".)
PythonRecrut
Вы правы

outf.write(b'\r\n'.join(p.retr(i + 1)[1]))
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB