Does Python's imaplib let you set a timeout?

The imaplib module doesn't provide a way to set timeout, but you can set a default timeout for new socket connections via the socket.setdefaulttimeout:

import socket
import imaplib
socket.setdefaulttimeout(10)
imap = imaplib.IMAP4('test.com', 666)

Or you can also go about overriding the imaplib.IMAP4 class with some knowledge from imaplib source and docs, which provides better control:

import imaplib
import socket

class IMAP(imaplib.IMAP4):
    def __init__(self, host='', port=imaplib.IMAP4_PORT, timeout=None):
        self.timeout = timeout
        # no super(), it's an old-style class
        imaplib.IMAP4.__init__(self, host, port)

    def open(self, host='', port=imaplib.IMAP4_PORT):
        self.host = host
        self.port = port
        self.sock = socket.create_connection((host, port), timeout=self.timeout)
        # clear timeout for socket.makefile, needs blocking mode
        self.sock.settimeout(None)
        self.file = self.sock.makefile('rb')

Note that after creating the connection we should set the socket timeout back to None to get it to blocking mode for subsequent socket.makefile call, as stated in the method docs:

... The socket must be in blocking mode (it can not have a timeout). ...


import imaplib
...
# ssl and timeout are config values
# connection to IMAP server
if ssl:
    imapResource = imaplib.IMAP4_SSL(server, port)
else:
    imapResource = imaplib.IMAP4(server, port)
# communications timeout
sock=imapResource.socket()
timeout = 60 * 5 # 5 minutes
sock.settimeout(timeout)

Tags:

Python

Imaplib