Skip to content

Commit 2eb9cb5

Browse files
committed
debugpy : Decode debugger IP address on connect.
Signed-off-by: Jos Verlinde <[email protected]>
1 parent 19883c1 commit 2eb9cb5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

python-ecosys/debugpy/debugpy/public_api.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Public API for debugpy."""
22

33
import socket
4+
import struct
45
import sys
56
from .common.constants import DEFAULT_HOST, DEFAULT_PORT
67
from .server.debug_session import DebugSession
@@ -43,7 +44,7 @@ def listen(port=DEFAULT_PORT, host=DEFAULT_HOST):
4344
client_sock = None
4445
try:
4546
client_sock, client_addr = listener.accept()
46-
print(f"Debugger connected from {client_addr}")
47+
print(f"Debugger connected from {format_client_addr(client_addr)}")
4748

4849
# Create debug session
4950
_debug_session = DebugSession(client_sock)
@@ -73,6 +74,26 @@ def listen(port=DEFAULT_PORT, host=DEFAULT_HOST):
7374

7475
return (host, port)
7576

77+
def format_client_addr(client_addr):
78+
"""Format client address using socket module methods"""
79+
if isinstance(client_addr, (tuple, list)):
80+
# Already in (ip, port) format
81+
return f"{client_addr[0]}:{client_addr[1]}"
82+
elif isinstance(client_addr, bytes) and len(client_addr) >= 8:
83+
# Extract port (bytes 2-4, network byte order)
84+
port = struct.unpack('!H', client_addr[2:4])[0]
85+
# Extract IP address (bytes 4-8) using inet_ntoa
86+
ip_packed = client_addr[4:8]
87+
try:
88+
# inet_ntoa expects 4-byte string in network byte order
89+
ip_addr = socket.inet_ntoa(ip_packed)
90+
return f"{ip_addr}:{port}"
91+
except:
92+
# Fallback if inet_ntoa not available (MicroPython)
93+
ip_addr = '.'.join(str(b) for b in ip_packed)
94+
return f"{ip_addr}:{port}"
95+
else:
96+
return str(client_addr)
7697

7798
def wait_for_client():
7899
"""Wait for the debugger client to connect and initialize."""

0 commit comments

Comments
 (0)