2015-10-30 1 views
1

Une partie de mon script:Comment intercepter le délai d'attente de socket dans Python 3?

def testConnection(self): 
    # This code doesn't work 
    try: 
     self.imap.login(self.user, self.password) 
     return True 
    except: 
     return False 

Lorsque je tente de se connecter avec imaplib au serveur de messagerie avec paramètres incorrects, le script se bloque toujours avec cette erreur:

Traceback (most recent call last): 
    File "./mail-notifier.py", line 198, in <module> 
    mail_check() 
    File "./mail-notifier.py", line 161, in mail_check 
    if (SettingsExist() == True and Mail().testConnection() == True): 
    File "./mail-notifier.py", line 142, in __init__ 
    self.imap = imaplib.IMAP4_SSL(settings.value("MailServer"), settings.value("Port")) 
    File "/usr/lib64/python3.4/imaplib.py", line 1221, in __init__ 
    IMAP4.__init__(self, host, port) 
    File "/usr/lib64/python3.4/imaplib.py", line 181, in __init__ 
    self.open(host, port) 
    File "/usr/lib64/python3.4/imaplib.py", line 1234, in open 
    IMAP4.open(self, host, port) 
    File "/usr/lib64/python3.4/imaplib.py", line 257, in open 
    self.sock = self._create_socket() 
    File "/usr/lib64/python3.4/imaplib.py", line 1224, in _create_socket 
    sock = IMAP4._create_socket(self) 
    File "/usr/lib64/python3.4/imaplib.py", line 247, in _create_socket 
    return socket.create_connection((self.host, self.port)) 
    File "/usr/lib64/python3.4/socket.py", line 512, in create_connection 
    raise err 
    File "/usr/lib64/python3.4/socket.py", line 503, in create_connection 
    sock.connect(sa) 
socket.timeout: timed out 

Je ne peux pas attraper exception de délai d'attente et imprimez un message d'erreur et continuez à travailler. Je pensais "sauf:" attrape toutes les erreurs qui se produisent. J'ai essayé de mettre "sauf socket.timeout:" mais sans succès. Qu'est-ce que j'ai mal?

+0

Le problème semble être dans l'établissement de la connexion, mais pas dans le délai de connexion. – hrust

+0

Comment faire pour attraper cette erreur? – Rinaldus

+0

Essayez d'attraper l'erreur là, où vous appelez: 'mail_check()' – hrust

Répondre

0

socket.connect(address)

Connect to a remote socket at address. (The format of address depends on the address family — see above.)

If the connection is interrupted by a signal, the method waits until the connection completes, or raise a socket.timeout on timeout, if the signal handler doesn’t raise an exception and the socket is blocking or has a timeout. For non-blocking sockets, the method raises an InterruptedError exception if the connection is interrupted by a signal (or the exception raised by the signal handler).

Changed in version 3.5: The method now waits until the connection completes instead of raising an InterruptedError exception if the connection is interrupted by a signal, the signal handler doesn’t raise an exception and the socket is blocking or has a timeout (see the PEP 475 for the rationale).

En cas de connexion à distance, vous devriez vérifier si la connexion Internet peut être établie (vous et destination à distance sont accessibles) et le réglage de connexion pour effectuer des actions que vous voulez sont corrects.