@@ -38,6 +38,7 @@ class APIClientArgs:
38
38
39
39
# port is set to None by default, but it gets replaced with 443 if not specified
40
40
# context possible values - web_api (default) or gaia_api
41
+ # single_conn is set to True by default, when work on parallel set to False
41
42
def __init__ (self , port = None , fingerprint = None , sid = None , server = "127.0.0.1" , http_debug_level = 0 ,
42
43
api_calls = None , debug_file = "" , proxy_host = None , proxy_port = 8080 ,
43
44
api_version = None , unsafe = False , unsafe_auto_accept = False , context = "web_api" , single_conn = True ):
@@ -123,6 +124,7 @@ def __exit__(self, exc_type, exc_value, traceback):
123
124
# if sid is not empty (the login api was called), then call logout
124
125
if self .sid :
125
126
self .api_call ("logout" )
127
+ self .close_connection ()
126
128
# save debug data with api calls to disk
127
129
self .save_debug_data ()
128
130
@@ -459,7 +461,7 @@ def get_server_fingerprint(self):
459
461
Initiates an HTTPS connection to the server if need and extracts the SHA1 fingerprint from the server's certificate.
460
462
:return: string with SHA1 fingerprint (all uppercase letters)
461
463
"""
462
- conn = self .get_https_connection ()
464
+ conn = self .get_https_connection (set_fingerprint = False , set_debug_level = False )
463
465
fingerprint_hash = conn .get_fingerprint_hash ()
464
466
if not self .single_conn :
465
467
conn .close ()
@@ -707,7 +709,7 @@ def read_fingerprint_from_file(server, filename="fingerprints.txt"):
707
709
return json_dict [server ]
708
710
return ""
709
711
710
- def create_https_connection (self ):
712
+ def create_https_connection (self , set_fingerprint , set_debug_level ):
711
713
context = ssl .create_default_context ()
712
714
context .check_hostname = False
713
715
context .verify_mode = ssl .CERT_NONE
@@ -719,19 +721,21 @@ def create_https_connection(self):
719
721
conn = HTTPSConnection (self .server , self .get_port (), context = context )
720
722
721
723
# Set fingerprint
722
- conn .fingerprint = self .fingerprint
724
+ if set_fingerprint :
725
+ conn .fingerprint = self .fingerprint
723
726
724
727
# Set debug level
725
- conn .set_debuglevel (self .http_debug_level )
728
+ if set_debug_level :
729
+ conn .set_debuglevel (self .http_debug_level )
726
730
conn .connect ()
727
731
return conn
728
732
729
- def get_https_connection (self ):
733
+ def get_https_connection (self , set_fingerprint = True , set_debug_level = True ):
730
734
if self .single_conn :
731
735
if self .conn is None :
732
- self .conn = self .create_https_connection ()
736
+ self .conn = self .create_https_connection (set_fingerprint , set_debug_level )
733
737
return self .conn
734
- return self .create_https_connection ()
738
+ return self .create_https_connection (set_fingerprint , set_debug_level )
735
739
736
740
def close_connection (self ):
737
741
if self .conn :
@@ -748,5 +752,7 @@ def connect(self):
748
752
self .sock = ssl .wrap_socket (self .sock , self .key_file , self .cert_file , cert_reqs = ssl .CERT_NONE )
749
753
750
754
def get_fingerprint_hash (self ):
755
+ if self .sock is None :
756
+ self .connect ()
751
757
fingerprint = hashlib .new ("SHA1" , self .sock .getpeercert (True )).hexdigest ()
752
758
return fingerprint .upper ()
0 commit comments