|
1 | 1 | """Public API for debugpy."""
|
2 | 2 |
|
3 | 3 | import socket
|
| 4 | +import struct |
4 | 5 | import sys
|
5 | 6 | from .common.constants import DEFAULT_HOST, DEFAULT_PORT
|
6 | 7 | from .server.debug_session import DebugSession
|
@@ -43,7 +44,7 @@ def listen(port=DEFAULT_PORT, host=DEFAULT_HOST):
|
43 | 44 | client_sock = None
|
44 | 45 | try:
|
45 | 46 | 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)}") |
47 | 48 |
|
48 | 49 | # Create debug session
|
49 | 50 | _debug_session = DebugSession(client_sock)
|
@@ -73,6 +74,26 @@ def listen(port=DEFAULT_PORT, host=DEFAULT_HOST):
|
73 | 74 |
|
74 | 75 | return (host, port)
|
75 | 76 |
|
| 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) |
76 | 97 |
|
77 | 98 | def wait_for_client():
|
78 | 99 | """Wait for the debugger client to connect and initialize."""
|
|
0 commit comments