@@ -211,19 +211,25 @@ def handle_tc_request(self, vendor_id, product_id):
211211
212212
213213def run_https_server (cert_file = "cert.pem" , key_file = "key.pem" ):
214- httpd = http .server .HTTPServer (
215- (DEFAULT_HOSTNAME , DEFAULT_PORT ), RESTRequestHandler )
216-
217- httpd .socket = ssl .wrap_socket (
218- httpd .socket ,
219- server_side = True ,
220- certfile = cert_file ,
221- keyfile = key_file ,
222- ssl_version = ssl .PROTOCOL_TLS ,
223- )
224-
225- print (f"Serving on https://{ DEFAULT_HOSTNAME } :{ DEFAULT_PORT } " )
226- httpd .serve_forever ()
214+ # Creates a basic HTTP server instance that listens on DEFAULT_HOSTNAME and DEFAULT_PORT
215+ # RESTRequestHandler handles incoming HTTP requests
216+ httpd = http .server .HTTPServer ((DEFAULT_HOSTNAME , DEFAULT_PORT ), RESTRequestHandler )
217+
218+ # Creates an SSL context using TLS protocol for secure communications
219+ context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
220+
221+ # Loads the SSL certificate and private key for the server
222+ # cert_file: contains the server's public certificate
223+ # key_file: contains the server's private key
224+ context .load_cert_chain (certfile = cert_file , keyfile = key_file )
225+
226+ # Uses a context manager (with statement) to wrap the HTTP server's socket with SSL
227+ # server_side=True indicates this is a server socket
228+ # The wrapped socket is automatically closed when exiting the with block
229+ with context .wrap_socket (httpd .socket , server_side = True ) as httpd .socket :
230+ print (f"Serving on https://{ DEFAULT_HOSTNAME } :{ DEFAULT_PORT } " )
231+ # Starts the server and runs indefinitely, handling incoming HTTPS requests
232+ httpd .serve_forever ()
227233
228234
229235# Generate self-signed certificates if needed
0 commit comments