FAQ
Hello,
I'm trying to gather some mail and save it. I don't get why it isn't saved
as expected.

==========================================================================
import poplib, socket, sys
from configparser import Error as ProtocolError
args= sys.argv[1:] # this is fake but is here as example
def Pop3_access(*args):
'''Accessing a pop server, doing a function and return a result'''
func= args[0]; args= args[1:]
try:
pop= poplib.POP3(srv_info[0])
pop.user(srv_info[1])
pop.pass_(srv_info[2])
except (poplib.error_proto):
raise ProtocolError
except (socket.timeout,socket.gaierror):
try: pop.quit()
except NameError: pass # it wasn't started
return
result= func(pop, args)
pop.quit()
return result
def func(pop, N):
... return pop.retr(N)
...
msg= Pop3_access(func, 4)
from io import BytesIO as B
fp= B()
for l in msg[1]:
... fp.write(l)
... fp.write('\n'.encode())
...
34
1
50
1
63
1
65
1
36
1
52
1
41
1
114
1
45
1
38
1
74
1
56
1
37
1
34
1
28
1
23
1
33
1
56
1
57
1
17
1
44
1
31
1
54
1
30
1
30
1

1
31
1

1
39
1

1
12
1
32
1
49
1

1
6
1
64
1
68
1

1
from mailbox import mbox as Mbx
mbx= Mbx('/tmp/mb', True)
mbx.add(fp)
mbx.get_message(0)
<mailbox.mboxMessage object at 0x1704d10>
print(mbx.get_message(0))

>>>
===================================================================

The result is an empty message, but the fp.getvalue() shows file-like
stream.
Am I missing some point?

--
goto /dev/null

Search Discussions

  • Steven D'Aprano at Jul 2, 2011 at 1:08 pm

    TheSaint wrote:

    Hello,
    I'm trying to gather some mail and save it. I don't get why it isn't saved
    as expected.
    Short answer: you need to seek the fp file object back to the start of the
    file by calling fp.seek(0). Otherwise, when you add it to the mail box, if
    gets EOF immediately and nothing gets added.

    More comments below:

    =========================================================================>
    import poplib, socket, sys
    from configparser import Error as ProtocolError
    args= sys.argv[1:] # this is fake but is here as example

    It makes it very hard for us to run your code when you paste the interactive
    session. For small snippets, it's okay to just post the interactive
    session, but for more substantial code, it is best to past the function
    definition ready for us to copy and paste:

    def foo():
    pass
    foo("arg")
    def Pop3_access(*args):
    '''Accessing a pop server, doing a function and return a result'''
    func= args[0]; args= args[1:]
    Why don't you declare func as a named argument?

    def Pop3_access(func, *args):

    try:
    pop= poplib.POP3(srv_info[0])
    pop.user(srv_info[1])
    pop.pass_(srv_info[2])
    except (poplib.error_proto):
    raise ProtocolError
    except (socket.timeout,socket.gaierror):
    try: pop.quit()
    except NameError: pass # it wasn't started
    return
    result= func(pop, args)
    pop.quit()
    return result
    def func(pop, N):
    ... return pop.retr(N)
    ...
    msg= Pop3_access(func, 4)
    from io import BytesIO as B
    fp= B()
    for l in msg[1]:
    ... fp.write(l)
    ... fp.write('\n'.encode())
    ...
    34
    1
    50
    1
    [...]


    For those wondering, as I was, what all these numbers are, the
    BytesIO.write() method returns the number of bytes written.

    At this point, fp is a file-like object containing a number of bytes, but
    the file pointer is at the end of the file, so when you read from it, it
    immediately gets EOF (i.e. empty).
    from io import BytesIO as B
    x = B()
    x.write(bytes("hello", 'ascii'))
    5
    x.read()
    b''

    But if you seek back to the beginning:
    x.seek(0)
    x.read()
    b'hello'


    from mailbox import mbox as Mbx
    *raises eyebrow*

    Do you need to rename mbox? You don't even save a keypress:
    shift-m b x and m b o x both take 4 keypresses.


    --
    Steven
  • TheSaint at Jul 2, 2011 at 2:35 pm
    Steven D'Aprano wrote:

    Thank you very much.
    But if you seek back to the beginning:
    x.seek(0)
    x.read()
    b'hello'
    Found the matter and *is* working
    I discover another problem:
    one message contains also a different encoding, but mostly it is not
    possible to represent that writing on the normale console output.
    from mailbox import mbox as Mbx
    raises eyebrow

    Do you need to rename mbox? You don't even save a keypress:
    shift-m b x and m b o x both take 4 keypresses.
    You stroke a right point :D
    I could just use its own name

    The code was not exactly run from the python shell. I was trying to make a
    *complete* picture from my module. In fact there aren't the arguments to
    access the pop server.
    I'd like to mention that I've looked into the output file, which shows only
    "From MAILER DEAMON" + date of the action, but no message at all.

    --
    goto /dev/null

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 2, '11 at 11:42a
activeJul 2, '11 at 2:35p
posts3
users2
websitepython.org

2 users in discussion

TheSaint: 2 posts Steven D'Aprano: 1 post

People

Translate

site design / logo © 2022 Grokbase