how to embed C code in python program?

AJINKYA picture AJINKYA · Oct 19, 2010 · Viewed 7.4k times · Source

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. i want to perform a ACK scan so need a raw socket.

So please help me.

thank you

Answer

Ryan Ginstrom picture Ryan Ginstrom · Oct 19, 2010

Please check out Cython. It makes it very easy to wrap C code.

This is adapted from the documentation on calling external C functions:

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.