2

I've read in a lot of places that C-libraries can be ported to or written in python using the ctypes module from the standard library.

I've gone through the help('ctypes') page and from what I could gather I can create some of the C structures in Python, but my question is how do I use these to access the underlying system calls? For eg. when trying to port something like 'sys/if.h' to Python?

Can someone point me to good resources/documentation regarding the same?

4

1 回答 1

3

如果您想访问系统调用,您可以执行以下操作:

>>> from ctypes import CDLL
>>> libc = CDLL('libc.so.6')
>>> print libc.strlen('abcde')
5

参考:http ://blog.bstpierre.org/using-pythons-ctypes-to-make-system-calls

或者(这是棘手的部分)

将此处概述的系统调用包装到您的 C 代码中:

如何在 linux 中重新实现(或包装)系统调用函数?

并且,然后编写一个将由 CTypes 使用的兼容源代码文件,如下所示:

http://www.scipy.org/Cookbook/Ctypes

我希望这有帮助。

于 2013-01-03T20:34:43.123 回答