在python中使用imaplib进行错误处理

时间:2011-01-05 16:31:31

标签: python error-handling imaplib

当我使用这一行时:

m = imaplib.IMAP4("some host")

我收到此错误:

 m = imaplib.IMAP4("some host")
  File "C:\Python25\lib\imaplib.py", line 163, in __init__
    self.open(host, port)
  File "C:\Python25\lib\imaplib.py", line 230, in open
    self.sock.connect((host, port))
  File "<string>", line 1, in connect
error: (10061, 'Connection refused')

如何针对此错误进行错误处理?

1 个答案:

答案 0 :(得分:2)

使用标准try/except块:

import socket

try:
    m = imaplib.IMAP4("some host")
except socket.error, e:
    print "Error opening IMAP connection: ", e