@@ -135,10 +135,13 @@ class IMAP4:
135135
136136 r"""IMAP4 client class.
137137
138- Instantiate with: IMAP4([host[, port]])
138+ Instantiate with: IMAP4([host[, port[, timeout=None] ]])
139139
140140 host - host's name (default: localhost);
141141 port - port number (default: standard IMAP4 port).
142+ timeout - socket timeout (default: None)
143+ If timeout is not given or is None,
144+ the global default socket timeout is used
142145
143146 All IMAP4rev1 commands are supported by methods of the same
144147 name (in lower-case).
@@ -181,7 +184,7 @@ class error(Exception): pass # Logical errors - debug required
181184 class abort (error ): pass # Service errors - close and retry
182185 class readonly (abort ): pass # Mailbox status changed to READ-ONLY
183186
184- def __init__ (self , host = '' , port = IMAP4_PORT ):
187+ def __init__ (self , host = '' , port = IMAP4_PORT , timeout = None ):
185188 self .debug = Debug
186189 self .state = 'LOGOUT'
187190 self .literal = None # A literal argument to a command
@@ -195,7 +198,7 @@ def __init__(self, host='', port=IMAP4_PORT):
195198
196199 # Open socket to server.
197200
198- self .open (host , port )
201+ self .open (host , port , timeout )
199202
200203 try :
201204 self ._connect ()
@@ -284,23 +287,28 @@ def __exit__(self, *args):
284287 # Overridable methods
285288
286289
287- def _create_socket (self ):
290+ def _create_socket (self , timeout ):
288291 # Default value of IMAP4.host is '', but socket.getaddrinfo()
289292 # (which is used by socket.create_connection()) expects None
290293 # as a default value for host.
294+ if timeout is not None and not timeout :
295+ raise ValueError ('Non-blocking socket (timeout=0) is not supported' )
291296 host = None if not self .host else self .host
292297 sys .audit ("imaplib.open" , self , self .host , self .port )
293- return socket .create_connection ((host , self .port ))
298+ address = (host , self .port )
299+ if timeout is not None :
300+ return socket .create_connection (address , timeout )
301+ return socket .create_connection (address )
294302
295- def open (self , host = '' , port = IMAP4_PORT ):
303+ def open (self , host = '' , port = IMAP4_PORT , timeout = None ):
296304 """Setup connection to remote server on "host:port"
297305 (default: localhost:standard IMAP4 port).
298306 This connection will be used by the routines:
299307 read, readline, send, shutdown.
300308 """
301309 self .host = host
302310 self .port = port
303- self .sock = self ._create_socket ()
311+ self .sock = self ._create_socket (timeout )
304312 self .file = self .sock .makefile ('rb' )
305313
306314
@@ -1261,7 +1269,7 @@ class IMAP4_SSL(IMAP4):
12611269
12621270 """IMAP4 client class over SSL connection
12631271
1264- Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context]]]]])
1272+ Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context[, timeout=None] ]]]]])
12651273
12661274 host - host's name (default: localhost);
12671275 port - port number (default: standard IMAP4 SSL port);
@@ -1271,13 +1279,15 @@ class IMAP4_SSL(IMAP4):
12711279 and private key (default: None)
12721280 Note: if ssl_context is provided, then parameters keyfile or
12731281 certfile should not be set otherwise ValueError is raised.
1282+ timeout - socket timeout (default: None) If timeout is not given or is None,
1283+ the global default socket timeout is used
12741284
12751285 for more documentation see the docstring of the parent class IMAP4.
12761286 """
12771287
12781288
12791289 def __init__ (self , host = '' , port = IMAP4_SSL_PORT , keyfile = None ,
1280- certfile = None , ssl_context = None ):
1290+ certfile = None , ssl_context = None , timeout = None ):
12811291 if ssl_context is not None and keyfile is not None :
12821292 raise ValueError ("ssl_context and keyfile arguments are mutually "
12831293 "exclusive" )
@@ -1294,20 +1304,20 @@ def __init__(self, host='', port=IMAP4_SSL_PORT, keyfile=None,
12941304 ssl_context = ssl ._create_stdlib_context (certfile = certfile ,
12951305 keyfile = keyfile )
12961306 self .ssl_context = ssl_context
1297- IMAP4 .__init__ (self , host , port )
1307+ IMAP4 .__init__ (self , host , port , timeout )
12981308
1299- def _create_socket (self ):
1300- sock = IMAP4 ._create_socket (self )
1309+ def _create_socket (self , timeout ):
1310+ sock = IMAP4 ._create_socket (self , timeout )
13011311 return self .ssl_context .wrap_socket (sock ,
13021312 server_hostname = self .host )
13031313
1304- def open (self , host = '' , port = IMAP4_SSL_PORT ):
1314+ def open (self , host = '' , port = IMAP4_SSL_PORT , timeout = None ):
13051315 """Setup connection to remote server on "host:port".
13061316 (default: localhost:standard IMAP4 SSL port).
13071317 This connection will be used by the routines:
13081318 read, readline, send, shutdown.
13091319 """
1310- IMAP4 .open (self , host , port )
1320+ IMAP4 .open (self , host , port , timeout )
13111321
13121322 __all__ .append ("IMAP4_SSL" )
13131323
@@ -1329,7 +1339,7 @@ def __init__(self, command):
13291339 IMAP4 .__init__ (self )
13301340
13311341
1332- def open (self , host = None , port = None ):
1342+ def open (self , host = None , port = None , timeout = None ):
13331343 """Setup a stream connection.
13341344 This connection will be used by the routines:
13351345 read, readline, send, shutdown.
0 commit comments