How to create a socket.socket() object from a socket fd?
grant.b.edwards at gmail.com
Sat Jan 21 17:41:31 EST 2017
On 2017-01-21, Grant Edwards <grant.b.edwards at gmail.com> wrote:
> Given a Unix file discriptor for an open TCP socket, I can't figure
> out how to create a python 2.7 socket object like those returned by
> Based on the docs, one might think that socket.fromfd() would do that
> (since the docs say that's what it does):
> That prompts a question: given a "socket object" as returned by
> socket.fromfd(), how does one create a "socket._socketobject object"
> as returned by socket.socket()?
Of course I figured it out immediately after spending 15 minutes
whinging about it.
help(socket.socket) gives you a hint:
| socket([family[, type[, proto]]]) -> socket object
| Methods defined here:
| __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...
context = ssl.create_default_context()
sock = socket.fromfd(fd2,socket.AF_INET,socket.SOCK_STREAM)
nsock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,_sock=sock)
conn = context.wrap_socket(nsock, server_hostname="whatever.invalid")
More information about the Python-list