Trying to understand rfc822.Message() behaviour

Discussion in 'Python' started by Phoe6, Nov 30, 2006.

  1. Phoe6

    Phoe6 Guest

    Hi all,
    Have a look at this snippet, I have a file direct.txt and I want to
    read it as rfc8222.Message() so that I get the Subject: and Mood: as
    Dict Keys and content separately, but I am unable to get the Content
    Properly.

    >>> fhandle = open('direct.txt','r')
    >>> print fhandle.read()

    Subject: testing - fortune
    Mood: happy


    "Why should we subsidize intellectual curiosity?"
    - Ronald Reagan


    >>> fhandle.seek(0)
    >>> import rfc822
    >>> message = rfc822.Message(fhandle)
    >>> print message

    Subject: testing - fortune
    Mood: happy

    >>>


    What is happening here. Why is the message not coming up?

    --
    Senthil
     
    Phoe6, Nov 30, 2006
    #1
    1. Advertising

  2. Phoe6 wrote:

    > Have a look at this snippet, I have a file direct.txt and I want to
    > read it as rfc8222.Message() so that I get the Subject: and Mood: as
    > Dict Keys and content separately, but I am unable to get the Content
    > Properly.
    >
    >>>> fhandle = open('direct.txt','r')
    >>>> print fhandle.read()

    > Subject: testing - fortune
    > Mood: happy
    >
    >
    > "Why should we subsidize intellectual curiosity?"
    > - Ronald Reagan
    >
    >
    >>>> fhandle.seek(0)
    >>>> import rfc822
    >>>> message = rfc822.Message(fhandle)
    >>>> print message

    > Subject: testing - fortune
    > Mood: happy
    >
    >
    > What is happening here. Why is the message not coming up?


    because the rfc822.Message parser only reads the header; to read the
    rest, just call "read" on the file object after you've parsed the header.

    see

    http://effbot.org/librarybook/rfc822.htm

    for some sample code.

    </F>
     
    Fredrik Lundh, Nov 30, 2006
    #2
    1. Advertising

  3. Phoe6

    Neil Cerutti Guest

    On 2006-11-30, Phoe6 <> wrote:
    > Hi all,
    > Have a look at this snippet, I have a file direct.txt and I want to
    > read it as rfc8222.Message() so that I get the Subject: and Mood: as
    > Dict Keys and content separately, but I am unable to get the Content
    > Properly.
    >
    >>>> fhandle = open('direct.txt','r')
    >>>> print fhandle.read()

    > Subject: testing - fortune
    > Mood: happy
    >
    >
    > "Why should we subsidize intellectual curiosity?"
    > - Ronald Reagan
    >
    >
    >>>> fhandle.seek(0)
    >>>> import rfc822
    >>>> message = rfc822.Message(fhandle)
    >>>> print message

    > Subject: testing - fortune
    > Mood: happy
    >
    >>>>

    >
    > What is happening here. Why is the message not coming up?


    From the Python Documentation 12.11.1 Message Objects:

    class Message( file[, seekable])

    A Message instance is instantiated with an input object as
    parameter. Message relies only on the input object having a
    readline() method; in particular, ordinary file objects
    qualify. Instantiation reads headers from the input object up
    to a delimiter line (normally a blank line) and stores them
    in the instance. The message body, following the headers, is
    not consumed.

    --
    Neil Cerutti
    We dispense with accuracy --sign at New York drug store
     
    Neil Cerutti, Nov 30, 2006
    #3
  4. Phoe6

    Phoe6 Guest

    Fredrik Lundh wrote:
    > Phoe6 wrote:
    >
    > > Have a look at this snippet, I have a file direct.txt and I want to
    > > read it as rfc8222.Message() so that I get the Subject: and Mood: as
    > > Dict Keys and content separately, but I am unable to get the Content
    > > Properly.

    >
    > because the rfc822.Message parser only reads the header; to read the
    > rest, just call "read" on the file object after you've parsed the header.
    >
    > see
    >
    > http://effbot.org/librarybook/rfc822.htm
    >



    Thank you. That helped me best to understand the rfc822 module.

    Thanks,
    Senthil
     
    Phoe6, Nov 30, 2006
    #4
  5. On 30 Nov 2006 10:22:17 -0800, "Phoe6" <> declaimed
    the following in comp.lang.python:


    > What is happening here. Why is the message not coming up?


    A quick test seems to indicate that Message only parses the headers.
    One is still responsible for reading the body using the open file...

    >>> message.headers

    ['Subject: testing - fortune\n', 'Mood: happy\n']
    >>> print f.readline()



    >>> print f.readline()

    "Why should we subsidize intellectual curiosity?"

    >>> print f.readline()

    - Ronald Reagan

    >>> message.rewindbody()
    >>> print f.readline()



    >>> print f.readline()

    "Why should we subsidize intellectual curiosity?"

    >>> print f.readline()

    - Ronald Reagan

    >>>

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Nov 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shilpa
    Replies:
    5
    Views:
    6,149
    akila_natarajan
    May 12, 2011
  2. John Doe
    Replies:
    1
    Views:
    323
    Steve Holden
    Aug 31, 2004
  3. praba kar
    Replies:
    1
    Views:
    765
    Jorgen Grahn
    Aug 7, 2005
  4. message/rfc822 and perl

    , Aug 29, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    147
    James Willmore
    Aug 30, 2003
  5. pdc
    Replies:
    1
    Views:
    87
Loading...

Share This Page