On this page
  1. socket.fromfd
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

socket.fromfd

Prev: socket.error | Up: socket | Next: socket.gaierror
Rel: socket.AddressFamily, socket.close, socket.create_connection, socket.create_server
Ref: docs.python.org — socket.fromfd
Tags: #public #python3 #stdlib #socket


Import

import socket
from socket import fromfd

Signature & Overview

socket.fromfd(fd, family, type, proto=0)

Create a socket object from a duplicate of the given file descriptor. The remaining arguments are the same as for socket().

Usage

import socket

s1, s2 = socket.socketpair()
try:
    cloned = socket.fromfd(s1.fileno(), s1.family, s1.type)
    with cloned:
        cloned.sendall(b"hi")
        assert s2.recv(2) == b"hi"
finally:
    s1.close(); s2.close()

Gotchas

REPL Inspect

from socket import fromfd
help(fromfd)

Linked from

Related notes