06 May, 2009, Zeno wrote in the 1st comment:
Votes: 0
I haven't worked much with Python before. Here's what I have in a script:
import getpass, poplib

M = poplib.POP3('mail.server.com')
M.user('user')
M.pass_('pass')
numMessages = len(M.list()[1])
print numMessages


I can do something like script.py > output.txt, but that only catches the print. I'm more focused on writing any possible errors to a file instead of the print line. Example:
zeno@zeno:~$ python script.py > test.txt
Traceback (most recent call last):
File "script.py", line 3, in <module>
M = poplib.POP3('mail.server.com')
File "/usr/local/lib/python2.5/poplib.py", line 84, in __init__
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
socket.gaierror: (-2, 'Name or service not known')

I need to ifcheck if that happens and write all of that to a different file. How would I do that?
06 May, 2009, David Haley wrote in the 2nd comment:
Votes: 0
You can redirect stderr like so:

python foo.py 2> stderr.txt

And you can redirect both streams separately as well:

python foo.py > stdout.txt 2> stderr.txt

Doing this from Python itself is probably more complicated.
06 May, 2009, Zeno wrote in the 3rd comment:
Votes: 0
Forgot about that. But I also need a timestamp.
06 May, 2009, David Haley wrote in the 4th comment:
Votes: 0
What exactly is it that you're trying to do here?

If you're trying to test for the error, why can't you catch it in the Python script?
06 May, 2009, Zeno wrote in the 5th comment:
Votes: 0
I want to connect to the POP server at intervals, and if it errors out write the error to a file with the exact time it happened at.
06 May, 2009, David Haley wrote in the 6th comment:
Votes: 0
OK, so forget about redirection and all that stuff. Just catch the error in Python and log the timestamp & error to some file.
06 May, 2009, Zeno wrote in the 7th comment:
Votes: 0
That's what I was asking about. I don't know how to do it and the POP3 lib page doesn't really explain it. I can do this:
try:
M = poplib.POP3('mail.server.com')
except:
print "Oops! Error"

But I need to figure out how to get the actual error.
06 May, 2009, David Haley wrote in the 8th comment:
Votes: 0
The basic idea is:

try:
foo
except Exception, e:
print(e)


This is a general python question, not really anything specific to the pop3 lib page (although you could get finer-grained exception catching by using the exact type etc.).

EDIT: as for getting timestamps, there's a library function to do that, but I don't remember it off-hand.
06 May, 2009, Zeno wrote in the 9th comment:
Votes: 0
Ah okay, I was trying to use exception poplib.error_proto. Confused me. :P
06 May, 2009, Zeno wrote in the 10th comment:
Votes: 0
I have this now:
M = poplib.POP3('mail.server.com', 110, 30)

Port 110, 30 sec timeout.
It's giving me this error:
Error:  __init__() takes at most 3 arguments (4 given)

Yet I only see 3 arguments…
06 May, 2009, elanthis wrote in the 11th comment:
Votes: 0
One of the arguments is the object itself, usually called self in the method. The version of Python you're using may have an older poplib that doesn't support the timeout argument. From the docs (http://docs.python.org/library/poplib.ht...) the timeout parameter wasn't added until Python 2.6.
07 May, 2009, Zeno wrote in the 12th comment:
Votes: 0
Thanks, didn't check that. Was using an older version.

What's a Python function that I can use to get the contents of a webpage? Like in PHP I use:
$goo = fopen("http://www.google.com", "rb") or die (fwrite($fh, $stringData1));
07 May, 2009, David Haley wrote in the 13th comment:
Votes: 0
See this page, then search for "url" – you'll find the a url library that works quite well. It's a good place to start looking in general.
13 May, 2009, Zeno wrote in the 14th comment:
Votes: 0
Got all that done, thanks. Trying to log this to a file now. Everything works except:
prnt = time.strftime("%a %b %d %H:%M:%S %Y")+"- 2/2 (Site Status:?) POP Error:"+e

I'm trying to have that a string, but apparently e can't be concatenated onto a string:
Traceback (most recent call last):
File "mailtest.py", line 30, in <module>
prnt = time.strftime("%a %b %d %H:%M:%S %Y")+"- 2/2 (Site Status:?) POP Error:"+e
TypeError: cannot concatenate 'str' and 'exceptions.TypeError' objects
13 May, 2009, David Haley wrote in the 15th comment:
Votes: 0
Yes, you need to stringify it first; Python is stricter about things like that than Perl or PHP. Just replace "e" with "str(e)".
0.0/15