what is recvmsg equivalent in python?

限于喜欢 提交于 2019-12-08 07:52:40

问题


I am implementing a python script to do fuse mount programatically. I have written an equivalent in C, by making use of socketpair and recvmsg api's. But in python recvmsg is not implemented, so I am stuck. Can anyone of you tell me a python equivalent of this? Any help would be appreciated.

Let me tell why do I need recvmsg, I require to send the fd of the fuse mount from the child to the parent.


回答1:


What you could look at is using pyx or a C module for Python that implements the required functionality in C and then you can manipulate and or use it from your Python script. This would allow you to send over the file descriptor and have the Python script act upon it.

Other things you can do is write a small C wrapper, that handles the recvmsg and not until it gets that fd and has opened it does it do a fork. All file descriptors that are open will stay open when you fork, and then exec or even just plain old exec meaning you don't have to worry about Python receiving it.




回答2:


As part of Python3 socket.recvmsg is now available in the socket module which, according to the docs, should allow you to do what you want (albeit rather late!):

On some systems, sendmsg() and recvmsg() can be used to pass file descriptors between processes over an AF_UNIX socket. When this facility is used (it is often restricted to SOCK_STREAM sockets), recvmsg() will return, in its ancillary data, items of the form (socket.SOL_SOCKET, socket.SCM_RIGHTS, fds), where fds is a bytes object representing the new file descriptors as a binary array of the native C int type. If recvmsg() raises an exception after the system call returns, it will first attempt to close any file descriptors received via this mechanism.



来源:https://stackoverflow.com/questions/6314982/what-is-recvmsg-equivalent-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!