Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > gmail/poplib: quickly detecting new mail

Reply
Thread Tools

gmail/poplib: quickly detecting new mail

 
 
LJ
Guest
Posts: n/a
 
      07-01-2006
Hello,

I'm trying to monitor my gmail account to know when I have obtained a
new email. It seems that once I have logged in, I should be able to
call the stat() function repeatedly to see how many messages are in my
inbox. The problem is that this number does not seem to update until I
have logged out, and logged back in. In other words, I run the code
below, send myself an email, and observe that the count does not
change. If I kill the program and restart (hence logging back in),
then the total count is now updated. The other function calls seem to
work the same way (eg "list" just shows the same list, even when I know
new mail has arrived).

Questions:
1. is this a standard behavior of pop protocol? (to give me the same
results for any API call on a single login?)
2. OR is this a peculiarity of gmail
3. is there a more efficient and correct way to see know when I have
received a new mail? Currently my "working" method is to log out and
log back in. With this method, I can get about 17 refreshes per minute
but anything faster and Gmail blocks me for a few minutes. (yes it is
important to my application that I have very frequent refreshes).

(see code sample below)

Thanks,
LJ

---

import poplib
import time
from email.Parser import Parser

parser = Parser()
server = poplib.POP3_SSL("pop.gmail.com", 995)
print server.user("XXX-MY_EMAIL")
print server.pass_("XXX-MY_PW")
server.set_debuglevel(0)

def getMsgCount():
# check message count by stat() and list() functions
numMsgs = server.stat()[0]
print "Num msg by stat():", numMsgs
print "Num msg by list():", len(server.list()[1])
print "Most recent:", numMsgs, getSubj(numMsgs)
return

def getSubj(which):
# return subject of message with id 'which'
msg = "\n".join(server.top(which, 1)[1])
email = parser.parsestr(msg)
return email.get("Subject")

while True:
print "--"
getMsgCount()
time.sleep(2)

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      07-01-2006
LJ <(E-Mail Removed)> wrote:
...
> 1. is this a standard behavior of pop protocol? (to give me the same
> results for any API call on a single login?)
> 2. OR is this a peculiarity of gmail


Definitely POP3 standard behavior, as a simple reading of the relevant
RFC will show (e.g., http://www.ietf.org/rfc/rfc1939.txt):
summarizing... on entering Transaction state, a POP3 server acquires an
exclusive lock on the mailbox, to ensure that no modifications occur to
it throughout the session.

I'm not sure about the refresh frequency, but you may want to try the
Atom feed, https://mail.google.com/gmail/feed/atom .


Alex
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New Python book, "Learn Python Quickly" John Rowland Python 0 03-16-2013 03:28 PM
How Quickly Can You Spot A New Woger Nym? Lawrence D'Oliveiro NZ Computing 2 02-28-2010 12:01 AM
New to Java (a C++ migrant) want to quickly get on to language Amol Java 4 01-19-2007 02:21 PM
New mail pop up! No new mail, still pops up! Moo Firefox 5 09-17-2006 06:49 PM
Detecting new mail? Robb Meade ASP General 3 08-29-2003 05:54 PM



Advertisments