How to create a socket.socket() object from a socket fd?

Grant Edwards grant.b.edwards at
Sat Jan 21 18:35:41 EST 2017

On 2017-01-21, Chris Angelico <rosuav at> wrote:
> On Sun, Jan 22, 2017 at 9:41 AM, Grant Edwards
><grant.b.edwards at> wrote:
>>  |  __init__(self, family=2, type=1, proto=0, _sock=None)
>>  |
>> Ah! There's a keyword argument that doesn't appear in the docs, so
>> let's try that...
> That's marginally better than my monkeypatch-after-creation
> suggestion, but still broadly the same. Your code may well break in
> other Python implementations, but within CPython 2.7, you should be
> pretty safe.

For those of you still paying attention...

It looks like CPython 3.4 socket.fromfd() does return the same type
object as socket.socket().  And, using the fileno= argument to
socket.socket() avoids duplicating the descriptor.

Looks like somebody with a time machine read my original post...


More information about the Python-list mailing list