Skip to content

Commit 506c877

Browse files
committed
PEP 748: create_connection and create_server instead of connect
The `connect()` function is poorly named server-side as it actually `bind()` the socket. Server-side the function lacks a `family` parameter to support IPv6 without a DNS lookup. Actually all three `connect()`, `bind()` and `listen()` socket function are pretty low-level. The high-level `create_connection` (for `connect`) and `create_server` (for `bind()` + `listen()`) are more pythonic. Wrap the latter and not the former, also to remove the need of a `listen()` method on the created socket (which is useless client-side).
1 parent 791d34e commit 506c877

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

peps/pep-0748.rst

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,15 @@ The ``ClientContext`` protocol class has the following class definition:
351351
...
352352
353353
@abstractmethod
354-
def connect(self, address: tuple[str | None, int]) -> TLSSocket:
354+
def create_connection(
355+
self,
356+
address: tuple[str | None, bytes | str | int | None],
357+
timeout: float | None = ...,
358+
source_address: _Address | None = None,
359+
*,
360+
all_errors: bool = False,
361+
sever_hostname: str | None = None,
362+
) -> TLSSocket:
355363
"""Creates a TLSSocket that behaves like a socket.socket, and
356364
contains information about the TLS exchange
357365
(cipher, negotiated_protocol, negotiated_tls_version, etc.).
@@ -382,9 +390,18 @@ The ``ServerContext`` protocol class has the following class definition:
382390
...
383391
384392
@abstractmethod
385-
def connect(self, address: tuple[str | None, int]) -> TLSSocket:
386-
"""Creates a TLSSocket that behaves like a socket.socket, and
387-
contains information about the TLS exchange
393+
def create_server(
394+
self,
395+
address: _Address,
396+
*,
397+
family: int = socket.AF_INET,
398+
backlog: int | None = None,
399+
reuse_port: bool = False,
400+
dualstack_ipv6: bool = False,
401+
) -> TLSSocket:
402+
"""Creates a listening socket with an accept() function that returns
403+
a TLSSocket that behaves like a socket.socket, and contains
404+
information about the TLS exchange
388405
(cipher, negotiated_protocol, negotiated_tls_version, etc.).
389406
"""
390407
...
@@ -404,7 +421,7 @@ specification of the ``TLSSocket`` protocol class. Specifically, implementations
404421
need to implement the following:
405422

406423
* ``recv`` and ``send``
407-
* ``listen`` and ``accept``
424+
* ``accept`` (server-side)
408425
* ``close``
409426
* ``getsockname``
410427
* ``getpeername``
@@ -458,13 +475,6 @@ The following code describes these functions in more detail:
458475
close_notify alert currently fails."""
459476
...
460477
461-
@abstractmethod
462-
def listen(self, backlog: int) -> None:
463-
"""Enable a server to accept connections. If backlog is specified, it
464-
specifies the number of unaccepted connections that the system will allow
465-
before refusing new connections."""
466-
...
467-
468478
@abstractmethod
469479
def accept(self) -> tuple[TLSSocket, tuple[str | None, int]]:
470480
"""Accept a connection. The socket must be bound to an address and listening

0 commit comments

Comments
 (0)