MCPcopy
hub / github.com/tornadoweb/tornado / bind_sockets

Function bind_sockets

tornado/netutil.py:56–187  ·  view source on GitHub ↗

Creates listening sockets bound to the given port and address. Returns a list of socket objects (multiple sockets are returned if the given address maps to multiple IP addresses, which is most common for mixed IPv4 and IPv6 use). Address may be either an IP address or hostname. If

(
    port: int,
    address: Optional[str] = None,
    family: socket.AddressFamily = socket.AF_UNSPEC,
    backlog: int = _DEFAULT_BACKLOG,
    flags: Optional[int] = None,
    reuse_port: bool = False,
)

Source from the content-addressed store, hash-verified

54
55
56def bind_sockets(
57 port: int,
58 address: Optional[str] = None,
59 family: socket.AddressFamily = socket.AF_UNSPEC,
60 backlog: int = _DEFAULT_BACKLOG,
61 flags: Optional[int] = None,
62 reuse_port: bool = False,
63) -> List[socket.socket]:
64 """Creates listening sockets bound to the given port and address.
65
66 Returns a list of socket objects (multiple sockets are returned if
67 the given address maps to multiple IP addresses, which is most common
68 for mixed IPv4 and IPv6 use).
69
70 Address may be either an IP address or hostname. If it's a hostname,
71 the server will listen on all IP addresses associated with the
72 name. Address may be an empty string or None to listen on all
73 available interfaces. Family may be set to either `socket.AF_INET`
74 or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise
75 both will be used if available.
76
77 The ``backlog`` argument has the same meaning as for
78 `socket.listen() <socket.socket.listen>`.
79
80 ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like
81 ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.
82
83 ``reuse_port`` option sets ``SO_REUSEPORT`` option for every socket
84 in the list. If your platform doesn&#x27;t support this option ValueError will
85 be raised.
86 """
87 if reuse_port and not hasattr(socket, "SO_REUSEPORT"):
88 raise ValueError("the platform doesn't support SO_REUSEPORT")
89
90 sockets = []
91 if address == "":
92 address = None
93 if not socket.has_ipv6 and family == socket.AF_UNSPEC:
94 # Python can be compiled with --disable-ipv6, which causes
95 # operations on AF_INET6 sockets to fail, but does not
96 # automatically exclude those results from getaddrinfo
97 # results.
98 # http://bugs.python.org/issue16208
99 family = socket.AF_INET
100 if flags is None:
101 flags = socket.AI_PASSIVE
102 bound_port = None
103 unique_addresses = set() # type: set
104 for res in sorted(
105 socket.getaddrinfo(address, port, family, socket.SOCK_STREAM, 0, flags),
106 key=lambda x: x[0],
107 ):
108 if res in unique_addresses:
109 continue
110
111 unique_addresses.add(res)
112
113 af, socktype, proto, canonname, sockaddr = res

Callers 6

listenMethod · 0.90
bindMethod · 0.90
__init__Method · 0.90
test_reuse_portMethod · 0.90
test_ipv6Method · 0.90

Calls 6

errno_from_exceptionFunction · 0.90
addMethod · 0.80
bindMethod · 0.80
appendMethod · 0.80
closeMethod · 0.45
listenMethod · 0.45

Tested by 4

__init__Method · 0.72
test_reuse_portMethod · 0.72
test_ipv6Method · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…