Skip to content

Commit 785c0c4

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 785c0c4

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

peps/pep-0748.rst

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,14 @@ 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, int],
357+
timeout=GLOBAL_DEFAULT,
358+
source_address=None,
359+
*,
360+
all_errors=False,
361+
) -> TLSSocket:
355362
"""Creates a TLSSocket that behaves like a socket.socket, and
356363
contains information about the TLS exchange
357364
(cipher, negotiated_protocol, negotiated_tls_version, etc.).
@@ -382,9 +389,18 @@ The ``ServerContext`` protocol class has the following class definition:
382389
...
383390
384391
@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
392+
def create_server(
393+
self,
394+
address: tuple[str | None, int],
395+
*,
396+
family=AF_INET,
397+
backlog=None,
398+
reuse_port=False,
399+
dualstack_ipv6=False,
400+
) -> TLSSocket:
401+
"""Creates a listening socket with an accept() function that returns
402+
a TLSSocket that behaves like a socket.socket, and contains
403+
information about the TLS exchange
388404
(cipher, negotiated_protocol, negotiated_tls_version, etc.).
389405
"""
390406
...
@@ -404,7 +420,7 @@ specification of the ``TLSSocket`` protocol class. Specifically, implementations
404420
need to implement the following:
405421

406422
* ``recv`` and ``send``
407-
* ``listen`` and ``accept``
423+
* ``accept`` (server-side)
408424
* ``close``
409425
* ``getsockname``
410426
* ``getpeername``
@@ -458,13 +474,6 @@ The following code describes these functions in more detail:
458474
close_notify alert currently fails."""
459475
...
460476
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-
468477
@abstractmethod
469478
def accept(self) -> tuple[TLSSocket, tuple[str | None, int]]:
470479
"""Accept a connection. The socket must be bound to an address and listening

0 commit comments

Comments
 (0)