简体   繁体   English

如何在python程序中嵌入C代码?

[英]how to embed C code in python program?

i want to write a program using multi-threading, raw sockets, to scan the ports in python i have a C code for injection of raw socket. 我想编写一个程序使用多线程,原始套接字,扫描python中的端口我有一个C代码注入原始套接字。 i want to perform a ACK scan so need a raw socket. 我想执行ACK扫描,因此需要一个原始套接字。

So please help me. 所以请帮助我。

thank you 谢谢

Please check out Cython . 请查看Cython It makes it very easy to wrap C code. 它使包装C代码变得非常容易。

This is adapted from the documentation on calling external C functions : 这是根据调用外部C函数文档改编的:

cdef extern from "math.h":
    double sin(double)

def pysin(x):
    return sin(x)

You could then call pysin from the compiled module like a normal Python module. 然后,您可以像编译普通的Python模块一样从已编译的模块调用pysin

I would definitely go with boost.python , which provides even cleaner wrappers. 我肯定会使用boost.python ,它提供更清晰的包装器。 If you dislike the idea of using C++, then Cython is a good alternative. 如果您不喜欢使用C ++,那么Cython是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM