diff --git a/.vscode/launch.json b/.vscode/launch.json index 898defc9c60fe1..b86e385f9921e4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,14 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Python Debugger: test_dcl_server", + "type": "debugpy", + "request": "launch", + "program": "/workspace/connectedhomeip/examples/chip-tool/commands/dcl/test_dcl_server.py", + "args": [], + "console": "integratedTerminal" + }, { "name": "Attach to running process", "type": "lldb", diff --git a/examples/chip-tool/commands/dcl/test_dcl_server.py b/examples/chip-tool/commands/dcl/test_dcl_server.py index f22f2d2a8e9390..1b68904b4f64b0 100755 --- a/examples/chip-tool/commands/dcl/test_dcl_server.py +++ b/examples/chip-tool/commands/dcl/test_dcl_server.py @@ -211,19 +211,25 @@ def handle_tc_request(self, vendor_id, product_id): def run_https_server(cert_file="cert.pem", key_file="key.pem"): - httpd = http.server.HTTPServer( - (DEFAULT_HOSTNAME, DEFAULT_PORT), RESTRequestHandler) - - httpd.socket = ssl.wrap_socket( - httpd.socket, - server_side=True, - certfile=cert_file, - keyfile=key_file, - ssl_version=ssl.PROTOCOL_TLS, - ) - - print(f"Serving on https://{DEFAULT_HOSTNAME}:{DEFAULT_PORT}") - httpd.serve_forever() + # Creates a basic HTTP server instance that listens on DEFAULT_HOSTNAME and DEFAULT_PORT + # RESTRequestHandler handles incoming HTTP requests + httpd = http.server.HTTPServer((DEFAULT_HOSTNAME, DEFAULT_PORT), RESTRequestHandler) + + # Creates an SSL context using TLS protocol for secure communications + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + + # Loads the SSL certificate and private key for the server + # cert_file: contains the server's public certificate + # key_file: contains the server's private key + context.load_cert_chain(certfile=cert_file, keyfile=key_file) + + # Uses a context manager (with statement) to wrap the HTTP server's socket with SSL + # server_side=True indicates this is a server socket + # The wrapped socket is automatically closed when exiting the with block + with context.wrap_socket(httpd.socket, server_side=True) as httpd.socket: + print(f"Serving on https://{DEFAULT_HOSTNAME}:{DEFAULT_PORT}") + # Starts the server and runs indefinitely, handling incoming HTTPS requests + httpd.serve_forever() # Generate self-signed certificates if needed