On this page
socket.socketpair
Prev: socket.SocketKind | Up: socket | Next: socket.SocketType
Rel: socket.AddressFamily, socket.close, socket.create_connection, socket.create_server
Ref: docs.python.org — socket.socketpair
Tags: #public #python3 #stdlib #socket
Import
import socket
from socket import socketpair
Signature & Overview
socket.socketpair(family=None, type=<SocketKind.SOCK_STREAM: 1>, proto=0)
Create a pair of socket objects from the sockets returned by the platform socketpair() function.
Usage
import socket
a, b = socket.socketpair()
with a, b:
a.sendall(b"ping")
assert b.recv(4) == b"ping"
Gotchas
- Connected in-memory pair of sockets: Great for unit-testing socket/TLS protocols or waking up
selectorsloops without binding a TCP port.
REPL Inspect
from socket import socketpair
help(socketpair)
Linked from
Related notes
- tuts/python3/stdlib_module/socket/SocketKind Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/SocketType Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/AddressFamily Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/close Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/create_connection Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/create_server Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/socket Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/socket/dup shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/error shared tags: #socket, #stdlib