如何在python中读取pop服务器响应



我正在尝试读取pop3 hotmail服务器的响应或异常。这是一个非常简单的问题,但我是python的初学者,不知道如何阅读它?这是我的代码:

import poplib
import sys
host = 'pop3.live.com'
port = 995
email='123456@hotmail.com'
pwd='123456'
server = poplib.POP3_SSL(host, port)
try:
    server.user(email)
    server.pass_(pwd)
    if('+OK'):
        print 'Email: '+email+'password: '+pwd
        server.quit()
        sys.exit(1)
except poplib.error_proto:
    if('POP+disabled'):
        print 'Email: '+email+'password: '+pwd
        server.quit()
        sys.exit(1)
    elif('authentication+failed'):
        print "wronge user and pass. try again"
        continue
    continue    

在例外情况下"if ('POP+disabled')"用于消除用户登录名和密码正确,但帐户未在选项中启用POP3。

当我运行上面的代码时,如果我输入了错误的密码,它也会显示电子邮件密码…

有谁能帮我处理这个问题吗?

在继续解析消息之前,可以使用server.getwelcome()方法检查服务器响应。

server对象允许您在身份验证后请求消息列表,然后您可以调用retr来检索每条消息。


    welcomeresp = server.getwelcome()
    if welcomeresp.find("+OK"):
       numMessages = len(server.list()[1])
       for i in range(numMessages): 
          for j in server.retr(i+1): 
              server_msg, body, octets = j
              for line in body:
                 print line

查看POP库的文档,了解更多信息和示例:

https://docs.python.org/2/library/poplib.html

最新更新