V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sbmzhcn
V2EX  ›  Python

python imaplib2 idle 问题?

  •  
  •   sbmzhcn · 2015-06-11 09:55:53 +08:00 · 2037 次点击
    这是一个创建于 3455 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原始代码 https://gist.github.com/jexhson/3496039

    import imaplib2, time
    from threading import *
    
    # This is the threading object that does all the waiting on 
    # the event
    class Idler(object):
        def __init__(self, conn):
            self.thread = Thread(target=self.idle)
            self.M = conn
            self.event = Event()
    
        def start(self):
            self.thread.start()
    
        def stop(self):
            # This is a neat trick to make thread end. Took me a 
            # while to figure that one out!
            self.event.set()
    
        def join(self):
            self.thread.join()
    
        def idle(self):
            # Starting an unending loop here
            while True:
                # This is part of the trick to make the loop stop 
                # when the stop() command is given
                if self.event.isSet():
                    return
                self.needsync = False
                # A callback method that gets called when a new 
                # email arrives. Very basic, but that's good.
                def callback(args):
                    if not self.event.isSet():
                        self.needsync = True
                        self.event.set()
                # Do the actual idle call. This returns immediately, 
                # since it's asynchronous.
                self.M.idle(callback=callback)
                # This waits until the event is set. The event is 
                # set by the callback, when the server 'answers' 
                # the idle call and the callback function gets 
                # called.
                self.event.wait()
                # Because the function sets the needsync variable,
                # this helps escape the loop without doing 
                # anything if the stop() is called. Kinda neat 
                # solution.
                if self.needsync:
                    self.event.clear()
                    self.dosync()
    
        # The method that gets called when a new email arrives. 
        # Replace it with something better.
        def dosync(self):
            print "Got an event!"
    
    # Had to do this stuff in a try-finally, since some testing 
    # went a little wrong.....
    try:
        # Set the following two lines to your creds and server
        M = imaplib2.IMAP4_SSL("mail.example.com")
        M.login("mylogin","mypassword")
        # We need to get out of the AUTH state, so we just select 
        # the INBOX.
        M.select("INBOX")
        # Start the Idler thread
        idler = Idler(M)
        idler.start()
        # Because this is just an example, exit after 1 minute.
        time.sleep(1*60)
    finally:
        # Clean up.
        idler.stop()
        idler.join()
        M.close()
        # This is important!
        M.logout()
    
    # The method that gets called when a new email arrives.
        # Replace it with something better.
        def dosync(self):
            print("Got an event!")
            a, b = self.M.SEARCH(None, 'UNSEEN')
            print("unseen:", a, b)
    

    在这一步,很多时候会执行到 print("Got an event!")这一步,说明有新邮件过来了,但接下来的代码self.M.SEARCH(None, 'UNSEEN') 获得的内容有时会是空的结果 OK [''], 正常情况下得到mail的id. OK ['778 745']

    有谁遇到过这样的问题,怎么解决?我想一旦有新邮件过来 ,就能获取到mail id,但有时获取不到。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5557 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 01:29 · PVG 09:29 · LAX 17:29 · JFK 20:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.