Reputation: 15389
There currently exists a socket library in Python:
Reference: http://docs.python.org/library/socket.html
However, this library does not include functionality for referencing an existing socket based off a file descriptor. I am currently developing Python bindings for a C++ library which opens up a socket first and need a way to reference this opened socket in Python after-the-fact. Keep in mind that this is on Windows (I am using a cross-platform layer of abstraction which neglects file handles).
The code is similar to this:
fd = connect(...) # file descriptor
os.read(fd, buffer)
os.close(fd)
I am looking to find a way to actually define a socket object based off the file descriptor without the by-value duplication found in os.dup
and os.dup2
.
Would anyone know a way to reference this?
Upvotes: 4
Views: 321
Reputation: 15389
As it doesn't appear that this question has been answered as of yet (for 1 week), I'll state my solution.
I implemented an internal binding for retrieving the socket information from within the library, converting the ctypes
to pytuples
. Because the application itself is wrapped in py2exe
, this modification is unobtrusive to the end-user.
Upvotes: 4
Reputation: 10985
socket.fromfd(fd, family, type[, proto])
seems to do what you want. It's only available on Unix like platforms, unfortunately.
Unfortunately, this functionality does not seem to be available for Windows. You may consider logging a bug at http://bugs.python.org/ (I see no reason why a windows socket object should not be wrappable in a Python socket object).
Upvotes: 0
Reputation: 2909
Does this help? You can use it to wrap sockets and/or file descriptors, so that they feel more like files, while providing operations useful in framing data like sock.readto('\0'):
http://stromberg.dnsalias.org/~strombrg/bufsock.html
Upvotes: 0
Reputation: 41663
Please check again:
http://docs.python.org/library/socket.html#socket.fromfd
Upvotes: 0