MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / format_sock

Function format_sock

test/functional/test_framework/netutil.py:215–244  ·  view source on GitHub ↗

Format either local or remote side of a socket to a human readable string, e.g. 1.2.3.4:8333 or [11:22::33]:8333 or /path/to/socket or @abstract-socket

(sock, *, local)

Source from the content-addressed store, hash-verified

213 return f"{addr}:{port}"
214
215def format_sock(sock, *, local):
216 '''
217 Format either local or remote side of a socket to a human readable string, e.g.
218 1.2.3.4:8333 or
219 [11:22::33]:8333 or
220 /path/to/socket or
221 @abstract-socket
222 '''
223 try:
224 if local:
225 name = sock.getsockname()
226 else:
227 name = sock.getpeername()
228 except Exception:
229 return "n/a"
230
231 if sock.family == socket.AF_INET:
232 return f"{name[0]}:{name[1]}"
233
234 if sock.family == socket.AF_INET6:
235 return f"[{name[0]}]:{name[1]}"
236
237 if sock.family == socket.AF_UNIX:
238 if isinstance(name, bytes):
239 name = name.decode(errors="backslashreplace")
240 if name.startswith("\0"):
241 return f"@{name[1:]}"
242 return name
243
244 return str(name)
245
246
247def set_ephemeral_port_range(sock):

Callers 2

forward_socketsFunction · 0.85
handleMethod · 0.85

Calls 1

decodeMethod · 0.45

Tested by

no test coverage detected