reusing sys.stdin? 
Author Message
 reusing sys.stdin?

Greetings,

Something I don't understand.  In my program I need to access
sys.stdin from two different functions in the following manner:

   import rfc822,sys
   message_headers = rfc822.Message(sys.stdin)

The problem is that after the first Message instance is created,
sys.stdin seems to be absorbed so that it is no longer around for the
second call to rfc822.Message().  Why is this, and is there a way to
keep sys.stdin for disappearing so I can use it a second time?

Regards,
Graham

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://www.*-*-*.com/



Tue, 30 Sep 2003 08:28:32 GMT  
 reusing sys.stdin?
The rfc822 module assumes that everything after the first blank line
is message body, and it does in fact "consume" it.  The file object
you give when instantiating the class winds up in the instance
variable fp.

In other words, your variable named "message_headers" actually
"contains" the entire message, not just the headers.

If you want blocks of header-like data without bodies, look at my
QChunk.py module; my key-data pairs are not completely RFC 822
compliant, but the code might give you some ideas.

All my projects (including QChunk.py) are at:

    http://newcenturycomputers.net/projects

On the other hand, if you are processing a mailbox, look into the
mailbox module.  It handles multiple messages in a single file or
file-like object.

Quote:
----- Original Message -----

Subject: reusing sys.stdin?

> Greetings,

> Something I don't understand.  In my program I need to access
> sys.stdin from two different functions in the following manner:

>    import rfc822,sys
>    message_headers = rfc822.Message(sys.stdin)

> The problem is that after the first Message instance is created,
> sys.stdin seems to be absorbed so that it is no longer around for the
> second call to rfc822.Message().  Why is this, and is there a way to
> keep sys.stdin for disappearing so I can use it a second time?

> Regards,
> Graham



Tue, 30 Sep 2003 10:54:48 GMT  
 reusing sys.stdin?
Hello,
    sys.stdin and sys.stdout are basically buffers; they're spaces of data
that you havn't gotten around to using yet. So after you do consume them,
they are indeed gone.

    You can save what is in them at any one point in time by just reading it
into a temporary variable for accessing later; the below will likely work
fine for you:

import rfc822, sys, cStringIO

stdin = cStringIO.StringIO(sys.stdin.read())

message_headers = rfc822.Message(stdin)

--Ix
(replace 'NOSPAM' with 'myseraph' to respond)

Quote:
> Greetings,

> Something I don't understand.  In my program I need to access
> sys.stdin from two different functions in the following manner:

>    import rfc822,sys
>    message_headers = rfc822.Message(sys.stdin)

> The problem is that after the first Message instance is created,
> sys.stdin seems to be absorbed so that it is no longer around for the
> second call to rfc822.Message().  Why is this, and is there a way to
> keep sys.stdin for disappearing so I can use it a second time?

> Regards,
> Graham

> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail.
> http://personal.mail.yahoo.com/

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----


Tue, 30 Sep 2003 12:25:05 GMT  
 reusing sys.stdin?

Quote:
> The rfc822 module assumes that everything after the first blank line
> is message body, and it does in fact "consume" it.  The file object
> you give when instantiating the class winds up in the instance
> variable fp.

In fact the Message method goes to considerable lengths *not* to consume the
data which follows the headers, leaving it up to you to read the body of the
message (using the fp instance variable of the message, for consistency).

Quote:
> In other words, your variable named "message_headers" actually
> "contains" the entire message, not just the headers.

This should not be the case. The problem is more likely that the code after
the call to rfc822.Message is assuming that everything up to end of file is
the message body, reading it, and therefore consuming the following message,
headers and all. If this is not the case then you are probably reading a
mailbox. In which case a look at the mailbox module wouldn't hurt.

Certainly you are likely to get problems reading RFC822-compliant mail
messages one after the other unless there is some concept of "structure"
(such as a mailbox) to help you separate the individual messages. The
standard define messages in terms of a data stream which ends at the end of
the body.

regards
 Steve

Quote:
> If you want blocks of header-like data without bodies, look at my
> QChunk.py module; my key-data pairs are not completely RFC 822
> compliant, but the code might give you some ideas.

> All my projects (including QChunk.py) are at:

>     http://newcenturycomputers.net/projects

> On the other hand, if you are processing a mailbox, look into the
> mailbox module.  It handles multiple messages in a single file or
> file-like object.

> ----- Original Message -----

> Subject: reusing sys.stdin?

> > Greetings,

> > Something I don't understand.  In my program I need to access
> > sys.stdin from two different functions in the following manner:

> >    import rfc822,sys
> >    message_headers = rfc822.Message(sys.stdin)

> > The problem is that after the first Message instance is created,
> > sys.stdin seems to be absorbed so that it is no longer around for the
> > second call to rfc822.Message().  Why is this, and is there a way to
> > keep sys.stdin for disappearing so I can use it a second time?

> > Regards,
> > Graham



Tue, 30 Sep 2003 19:23:50 GMT  
 reusing sys.stdin?


[ ... ]

Quote:
> In fact the Message method goes to considerable lengths *not* to consume

the

when he should have written

In fact the Message __init__() method ...



Tue, 30 Sep 2003 19:32:55 GMT  
 reusing sys.stdin?

Quote:

> You can save what is in them at any one point in time by just reading it
> into a temporary variable for accessing later; the below will likely work
> fine for you:

> import rfc822, sys, cStringIO

> stdin = cStringIO.StringIO(sys.stdin.read())

> message_headers = rfc822.Message(stdin)

Yes, it does.  Thanks so much!

Cheers,
Graham

_____________________________________________________________________________
http://movies.yahoo.com.au - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Fri, 03 Oct 2003 06:44:59 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

2. Chosing between sys.argv and sys.stdin

3. non-blocking sys.stdin.read() on win32?

4. banging head against sys.stdin and tkinter on win32

5. sys.stdin.readlines() in 2.2a ?

6. sys.stdin.read() HELP!

7. how to calculate the size of sys.stdin?

8. Error using sys.stdin.read()

9. can i make sys.stdin.read() not wait?

10. sys.stdin.readline

11. Is there something magic about sys.stdin?

12. line buffering sys.stdin

 

 
Powered by phpBB® Forum Software