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,
)
| 54 | |
| 55 | |
| 56 | def 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'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 |
searching dependent graphs…