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)
| 213 | return f"{addr}:{port}" |
| 214 | |
| 215 | def 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 | |
| 247 | def set_ephemeral_port_range(sock): |
no test coverage detected