POP3 Mail Download

K

Kevin F

Having some troubles downloading messages with POP3...

I can connect to the server just fine and list messages without any
problem with the following code:

------------------------
from poplib import *

server = POP3("mail.bluebottle.com")
print server.getwelcome()
print server.user("(e-mail address removed)")
print server.pass_("xxxxxxxx")

messagesInfo = server.list()[1]
numMessages = len(messagesInfo)

print numMessages
------------------------



However, if I try to actually download the messages themselves, my
python editor highlights 'msgSize' and says "invalid syntax" when I run
the following subsequent lines of code:


------------------------
emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)
 
D

Dennis Lee Bieber

However, if I try to actually download the messages themselves, my
python editor highlights 'msgSize' and says "invalid syntax" when I run
the following subsequent lines of code:


------------------------
emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)

Look very closely at your indentation


--
 
K

Kevin F

Dennis said:
However, if I try to actually download the messages themselves, my
python editor highlights 'msgSize' and says "invalid syntax" when I run
the following subsequent lines of code:


------------------------
emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)

Look very closely at your indentation

I fixed the indentation to:

emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)


and it still doesn't work, what's wrong?
 
P

Paul McGuire

Kevin F said:
Dennis said:
However, if I try to actually download the messages themselves, my
python editor highlights 'msgSize' and says "invalid syntax" when I run
the following subsequent lines of code:


------------------------
emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)

Look very closely at your indentation

I fixed the indentation to:

emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)


and it still doesn't work, what's wrong?

See comments.
-- Paul


emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0] # line is missing closing paren
msgSize = int(split(msg, " ")[1] # line is missing closing paren,
too
if(msgSize < 20000): # this line should not be
further indented
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)
 
K

Kevin F

Paul said:
Kevin F said:
Dennis said:
However, if I try to actually download the messages themselves, my
python editor highlights 'msgSize' and says "invalid syntax" when I run
the following subsequent lines of code:


------------------------
emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)
Look very closely at your indentation
I fixed the indentation to:

emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]
if(msgSize < 20000):
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)


and it still doesn't work, what's wrong?

See comments.
-- Paul


emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0] # line is missing closing paren
msgSize = int(split(msg, " ")[1] # line is missing closing paren,
too
if(msgSize < 20000): # this line should not be
further indented
messages = server.retr(msgNum)[1]
messages = join(message, "\n")
emails.append(message)
thanks, that helped fix the syntax error, however, now when i run it, it
gives me an error for 'split', saying "name 'split' is not defined"

i'm new to python so bear with me but how do i fix this?
 
B

Bob Piton

Dennis said:
I fixed the indentation to:

emails = []
for msg in messagesInfo:
msgNum = int(split(msg, " ")[0]
msgSize = int(split(msg, " ")[1]

Now look at your parentheses...
and it still doesn't work, what's wrong?
msgNum = int(split(msg, " ")[0]
HINT: 0 1 2 1 ?
i have no idea what you are hinting at, can you please just tell me what i
need to change?

I know how you feel; it's brutal trying to get elementary information from
this group.

I think what he is hinting at is that you are missing a right parentheses.

msgNum = int(split(msg, " ")[0]

should be:

msgNum = int(split(msg, " "))[0]


msgSize = int(split(msg, " ")[1]

should be:

msgSize = int(split(msg, " "))[1]


Now if only somebody would tell me, with elementary examples, how you
write to the thing called 'stdout' and how you read from 'stdin'.
 
A

Alex Martelli

Kevin F said:
msgSize = int(split(msg, " ")[1] # line is missing closing paren,
...
thanks, that helped fix the syntax error, however, now when i run it, it
gives me an error for 'split', saying "name 'split' is not defined"

i'm new to python so bear with me but how do i fix this?

you change the above-quoted assignment into:

msgSize = int(msg.split()[1])

and similarly for other occurrences of split, which is a string method
(and needs to be called as such) and not a builtin function.


Alex
 
K

Kent Johnson

Bob said:
I know how you feel; it's brutal trying to get elementary information from
this group.

You could try the python-tutor list, it tends to give more focused
answers to newbie questions than this group, which is pretty
newbie-friendly but does have a tendency to veer widely from the
original question.
Now if only somebody would tell me, with elementary examples, how you
write to the thing called 'stdout' and how you read from 'stdin'.

raw_input() prompts to stdout and reads from stdin. print outputs to stdout:

In [53]: print raw_input('What is your name? ')
What is your name? Kent
Kent

or import sys and use sys.stdin and sys.stdout.

Kent
 
S

Scott David Daniels

Bob said:
I think what he is hinting at is that you are missing a right parentheses.

msgNum = int(split(msg, " ")[0]
should be:
msgNum = int(split(msg, " "))[0]
Or more likely:
msgNum = int(split(msg, " ")[0])
msgSize = int(split(msg, " ")[1]
should be:
msgSize = int(split(msg, " "))[1]
Similarly:
msgSize = int(split(msg, " ")[0])

More readably:
msgNum, msgSize = [int(text) for text in split(msg, " ")[:2]]
Now if only somebody would tell me, with elementary examples, how you
write to the thing called 'stdout' and how you read from 'stdin'.
import sys
print 'parrot' # writes to sys.stdout
print >>None, 'limburger' # Also writes to sys.stdout
print >>sys.stdout, 'roquefort' # Also writes to sys.stdout
sys.stdout.write('Shropshire -- the cheese of the gods\n) # Also

oneline = raw_input('prompt: ') # reads from sys.stdin
for line in sys.stdin: # reads line from sys.stdin
print 'The current line is: %r' % line
if not line.strip():
break
chunk = sys.stdin.read(23) # reads a series of bytes from sys.stdin

Warning: idle does not implement line iteration as in the for loop.
Also the read in both line iteration and .read(N) may well read the
input in "block mode", so you may have to type-ahead or end with an
end-of-file (^D for Unix, ^Z for Windows) before it starts processing
the lines.

--Scott David Daniels
(e-mail address removed)
 
D

Dennis Lee Bieber

msgNum = int(split(msg, " ")[0]
HINT: 0 1 2 1 ?
i have no idea what you are hinting at, can you please just tell me what
i need to change?

The line starts with 0 parentheses... then you add 1 for each ( and
subtract 1 for each )... The line should end with 0.

I would also suggest that you read the standard documentation -- the
tutorial at least, and skim the first few chapters of the library
reference manual.

There is no such thing as a standalone "split" function. On older
Python versions you had to

import string

and use
xxx = string.split(sourcestring, delimiter)

newer Python versions make the operation a native method of string data
types, so you don't need the import, but use

xxx = sourcestring.split(delimiter)



comp.lang.python /is/ one of the friendliest groups available, but I
suspect you will find even this crowd will get tired of a sequence of
posts on the nature of: "it doesn't work" -- especially when they are
all on same single line of code.

It is preferable that, besides just saying one is new to the
language, one describe just what other stuff has been tried to figure
out a problem. IE, what edits to the code did one try. Which manuals did
one look at... Did one do a google search for the error message that is
displayed? (and one should typically cut&paste the Python error trace
when asking for help).
--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top