有人可以告诉我这段代码的用途

时间:2015-01-14 05:08:29

标签: python-3.x

import imaplib,time

class Mail():
    def __init__(self):
        self.user= 'USERNAME'
        self.password= 'PASSWORD'
        self.M = imaplib.IMAP4_SSL('imap.gmail.com', '993')
        self.M.login(self.user, self.password)

    def checkMail(self):
        self.M.select()
        self.unRead = self.M.search(None, 'UnSeen')
        return len(self.unRead[1][0].split())



email = Mail()

while 1:
    print ('Sending')
    time.sleep(2)

1 个答案:

答案 0 :(得分:0)

您初始化名为class的{​​{1}},该Mail应该使用imaplib库来检查Gmail帐户的未读邮件。但是,在将email分配给Mail的实例后,负责代码中操作的while循环由两个事件组成; printtime.sleep()

因此,您的代码基本上每2秒打印一次'Sending'

运行 在因凭据无效而注释掉email = Mail()行后

bash-3.2$ python foo.py
Sending
Sending
^CTraceback (most recent call last):
  File "foo.py", line 21, in <module>
    time.sleep(2)
KeyboardInterrupt
bash-3.2$