[IronPython] poplib.POP3_SSL OutOfMemory Issue
matan keret
matan504 at gmail.com
Sun Nov 15 17:34:14 CET 2009
Hi everyone!
i wrote the next code which suppose to go to my hotmail account, fetch the
last email's image attachment and save it to a file:
import email, poplib, os, string
def get_messages_id(message_list):
items = []
for message in message_list[1]:
message_parts = string.split(message,' ')
message_id = message_parts[0]
items.append(message_id)
# print 'message id is: %s\n' % message_id
# print 'done getting ids'
return items
host = "pop3.live.com"
detach_dir = 'D:\\test-attachments\\' # directory where to save attachments
(default: current)
user = "matan.py at hotmail.com"
pwd = "0okmnji9"
server = poplib.POP3_SSL(host)
server.user(user)
server.pass_(pwd)
numMessages = len(server.list()[1])
#print numMessages
# server.list() returns messages info from server in
# form of response, message_list, size where message_list
# is. a list of messages in form of 'message_id size'
message_list = server.list()
# get the list of items id
items = get_messages_id(message_list)
items.reverse() # puts the last received mail's id in the first place in
list
is_image = False
# find the first mail in items that has an attachment and get the attachment
for emailid in items:
# if we found an email with an image we break
if is_image:
break
total_email_message = server.retr(emailid)[1]
messageText = string.join(total_email_message, "\n")
mail = email.message_from_string(messageText) # parsing the mail content
to get a mail object
# Check if there are any attachments at all
if mail.get_content_maintype() != 'multipart':
continue
# we use walk to create a generator so we can iterate on the parts
for part in mail.walk():
# skip any attachment which is not an image
if part.get_content_maintype() != 'image':
continue
# is this part an attachment ?
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
is_image = True
# if there is no filename, we create one with a counter to avoid
duplicates
if not filename:
filename = 'part-%03d%s' % (1, 'bin')
att_path = os.path.join(detach_dir, filename)
# Check if its not already there and finally write the file
if not os.path.isfile(att_path) :
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
# if we found an email with an image we break after first image
downloaded
if is_image:
break
server.quit()
print 'Finished fetching the image'
# THE END
also available at: http://pastebin.org/53873
This works fine on Cpython but on IronPython 2.0.3 I get:
Traceback (most recent call last):
File "get_first_attachment.py", line 21, in get_first_attachment.py
File "D:\IronPython 2.0.3\Lib\poplib.py", line 361, in __init__
File "D:\IronPython 2.0.3\Lib\poplib.py", line 137, in _getresp
File "D:\IronPython 2.0.3\Lib\poplib.py", line 374, in _getline
File "D:\IronPython 2.0.3\Lib\poplib.py", line 364, in _fillBuffer
MemoryError: Exception of type 'System.OutOfMemoryException' was thrown.
this comes out from line 21:
server = poplib.POP3_SSL(host)
Any ideas why is that? How to fix it? Is it a known issue (tried to look but
couldn't find)?
Thanks,
Matan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091115/337bb95d/attachment.html>
More information about the Ironpython-users
mailing list