6
6
__author__ = 'Simon Robinson'
7
7
__copyright__ = 'Copyright (c) 2022 Simon Robinson'
8
8
__license__ = 'Apache 2.0'
9
- __version__ = '2023-01-21 ' # ISO 8601 (YYYY-MM-DD)
9
+ __version__ = '2023-02-08 ' # ISO 8601 (YYYY-MM-DD)
10
10
11
11
import argparse
12
12
import base64
@@ -805,20 +805,17 @@ def _ssl_handshake(self):
805
805
# see: https://github.com/python/cpython/issues/54293
806
806
try :
807
807
self .socket .do_handshake ()
808
- except self .ssl_handshake_errors as e :
809
- self ._ssl_handshake_error (e )
810
- else :
811
- Log .debug (self .info_string (), '<-> [' , self .socket .version (), 'handshake complete ]' )
812
- self .ssl_handshake_attempts = 0
813
- self .ssl_handshake_completed = True
814
-
815
- def _ssl_handshake_error (self , error ):
816
- if isinstance (error , ssl .SSLWantReadError ):
808
+ except ssl .SSLWantReadError :
817
809
select .select ([self .socket ], [], [], 0.01 ) # wait for the socket to be readable (10ms timeout)
818
- elif isinstance ( error , ssl .SSLWantWriteError ) :
810
+ except ssl .SSLWantWriteError :
819
811
select .select ([], [self .socket ], [], 0.01 ) # wait for the socket to be writable (10ms timeout)
820
- else :
812
+ except self . ssl_handshake_errors : # also includes SSLWant[Read/Write]Error, but already handled above
821
813
self .handle_close ()
814
+ else :
815
+ if not self .ssl_handshake_completed : # only notify once (we may need to repeat the handshake later)
816
+ Log .debug (self .info_string (), '<-> [' , self .socket .version (), 'handshake complete ]' )
817
+ self .ssl_handshake_attempts = 0
818
+ self .ssl_handshake_completed = True
822
819
823
820
def handle_read_event (self ):
824
821
# additional Exceptions are propagated to handle_error(); no need to handle here
@@ -830,8 +827,8 @@ def handle_read_event(self):
830
827
# have to deal with both unsecured, wrapped *and* STARTTLS-type sockets would only need this in recv/send
831
828
try :
832
829
super ().handle_read_event ()
833
- except self .ssl_handshake_errors as e :
834
- self ._ssl_handshake_error ( e )
830
+ except self .ssl_handshake_errors :
831
+ self ._ssl_handshake ( )
835
832
836
833
def handle_write_event (self ):
837
834
# additional Exceptions are propagated to handle_error(); no need to handle here
@@ -841,23 +838,23 @@ def handle_write_event(self):
841
838
# as in handle_read_event, we need to handle SSL handshake events
842
839
try :
843
840
super ().handle_write_event ()
844
- except self .ssl_handshake_errors as e :
845
- self ._ssl_handshake_error ( e )
841
+ except self .ssl_handshake_errors :
842
+ self ._ssl_handshake ( )
846
843
847
844
def recv (self , buffer_size ):
848
845
# additional Exceptions are propagated to handle_error(); no need to handle here
849
846
try :
850
847
return super ().recv (buffer_size )
851
- except self .ssl_handshake_errors as e :
852
- self ._ssl_handshake_error ( e )
848
+ except self .ssl_handshake_errors :
849
+ self ._ssl_handshake ( )
853
850
return b''
854
851
855
852
def send (self , byte_data ):
856
853
# additional Exceptions are propagated to handle_error(); no need to handle here
857
854
try :
858
855
return super ().send (byte_data ) # buffers before sending via the socket, so failure is okay; will auto-retry
859
- except self .ssl_handshake_errors as e :
860
- self ._ssl_handshake_error ( e )
856
+ except self .ssl_handshake_errors :
857
+ self ._ssl_handshake ( )
861
858
return 0
862
859
863
860
def handle_error (self ):
0 commit comments