Skip to content

Commit c92f5dc

Browse files
committed
Support single https connection
1 parent 114b3ea commit c92f5dc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

cpapi/mgmt_api.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class APIClientArgs:
3838

3939
# port is set to None by default, but it gets replaced with 443 if not specified
4040
# 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
4142
def __init__(self, port=None, fingerprint=None, sid=None, server="127.0.0.1", http_debug_level=0,
4243
api_calls=None, debug_file="", proxy_host=None, proxy_port=8080,
4344
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):
123124
# if sid is not empty (the login api was called), then call logout
124125
if self.sid:
125126
self.api_call("logout")
127+
self.close_connection()
126128
# save debug data with api calls to disk
127129
self.save_debug_data()
128130

@@ -459,7 +461,7 @@ def get_server_fingerprint(self):
459461
Initiates an HTTPS connection to the server if need and extracts the SHA1 fingerprint from the server's certificate.
460462
:return: string with SHA1 fingerprint (all uppercase letters)
461463
"""
462-
conn = self.get_https_connection()
464+
conn = self.get_https_connection(set_fingerprint=False, set_debug_level=False)
463465
fingerprint_hash = conn.get_fingerprint_hash()
464466
if not self.single_conn:
465467
conn.close()
@@ -707,7 +709,7 @@ def read_fingerprint_from_file(server, filename="fingerprints.txt"):
707709
return json_dict[server]
708710
return ""
709711

710-
def create_https_connection(self):
712+
def create_https_connection(self, set_fingerprint, set_debug_level):
711713
context = ssl.create_default_context()
712714
context.check_hostname = False
713715
context.verify_mode = ssl.CERT_NONE
@@ -719,19 +721,21 @@ def create_https_connection(self):
719721
conn = HTTPSConnection(self.server, self.get_port(), context=context)
720722

721723
# Set fingerprint
722-
conn.fingerprint = self.fingerprint
724+
if set_fingerprint:
725+
conn.fingerprint = self.fingerprint
723726

724727
# Set debug level
725-
conn.set_debuglevel(self.http_debug_level)
728+
if set_debug_level:
729+
conn.set_debuglevel(self.http_debug_level)
726730
conn.connect()
727731
return conn
728732

729-
def get_https_connection(self):
733+
def get_https_connection(self, set_fingerprint=True, set_debug_level=True):
730734
if self.single_conn:
731735
if self.conn is None:
732-
self.conn = self.create_https_connection()
736+
self.conn = self.create_https_connection(set_fingerprint, set_debug_level)
733737
return self.conn
734-
return self.create_https_connection()
738+
return self.create_https_connection(set_fingerprint, set_debug_level)
735739

736740
def close_connection(self):
737741
if self.conn:
@@ -748,5 +752,7 @@ def connect(self):
748752
self.sock = ssl.wrap_socket(self.sock, self.key_file, self.cert_file, cert_reqs=ssl.CERT_NONE)
749753

750754
def get_fingerprint_hash(self):
755+
if self.sock is None:
756+
self.connect()
751757
fingerprint = hashlib.new("SHA1", self.sock.getpeercert(True)).hexdigest()
752758
return fingerprint.upper()

0 commit comments

Comments
 (0)