Does Python's imaplib let you set a timeout? -


i'm looking @ api python's imaplib.

from can tell, there no way setup timeout can smtplib.

is correct? how handle host that's invalid if don't want block?

the imaplib module doesn't provide way set timeout, can set default timeout new socket connections via socket.setdefaulttimeout:

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

or can go overriding imaplib.imap4 class knowledge imaplib source , docs, 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 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 socket.makefile, needs blocking mode         self.sock.settimeout(none)         self.file = self.sock.makefile('rb') 

note after creating connection should set socket timeout none blocking mode subsequent socket.makefile call, stated in method docs:

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


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -