Thursday, November 12, 2015

How to use C code in Python

Python and C combination is brilliant for programmers specially who writing network tools and exploits. For this purpose there are many ways to use both of them in one code and make the use of both languages. Previous days I was looking for a easy way(not easy actually fast way to approach my object which is make use of c code inside a python code) to do this. I was noticed there are many ways to do this. Some of them are,

  1. According to Extending Python with C doc (https://docs.python.org/2/extending/extending.html).
  2. Some languages has built in combine with features of these two languages such as Pyrex, Cython.
  3. Using ctype module.
  4. Using a SWIG (Simple Wrapper Interface Generator)



There are other ways also to achieve this but I was impressed by using SWIG. It's so easy and simple to wrap the code. So here is the steps.

I'm going to wrap a simple C function called "func" on the C code "hello.c"

1 Step : Create hello.c

following is the code in hello.c

#include <stdio.h>

int func(){
printf("Hello World");
return 0;
}
 
2nd Step : Create interface file.





To add your C code to your desired language's code you need to create interface file which is input to SWIG. Here is the interface file which is "hello.i".

%module hello
%{
int func();
%}
int func();

As you can see at the first line you have to name the module you are going to create. In this case hello module will create. This is very similar to write C code. After you put brace on second line the block has been started. Inside this block you have to mention all of your headers and functions in your C code. Here I'm using C default header stdio.h so it's no need to mention in here. If you are using custom header with macros you need put it here like #include "example.h". Save the file.

3rd Step : Create setup.py file.

After 2nd step there are numerous ways to build you the shared object (so) file (in windows dll). But here I'm going to demonstrate build the so file using setup.py file. Following is the code for setup.py



#!/usr/bin/env python 
 from distutils.core import setup, Extension  
hello_module = Extension('_hello', sources=['hello_wrap.c', 'hello.c'], ) 
 setup (name = 'anyname', 
            version = '0.1', 
            author = "your name", 
            description = """docs""", 
            ext_modules = [hello_module], 
            py_modules = ["hello"], )
 
 
Be careful about bold items. First bold item type any variable name. In second bold item type "_" as prefix to your c file name. It's important. Third bold item ; when you run swig with hello.i it will generate file called "hello_wrap.c". This file is mentioned here. Forth bold item is your source code to wrap. Inside setup module fifth bold item is the variable that the first bold item. Sixth bold item is important because this module name will use when we importing module.

4th Step : run SWIG.




Type the following in shell to get the hello_wrap.c create the so file.

swig -python hello.i

5th Step : Run setup.py

To build our so file and make use the c code inside python code lets run setup.py

python setup.py build_ext --inplace

build_ext - this will build extensions
--inplace - build the so file in same directory shell opened. 



Now you can import the module that you coded from c like this

import hello

hello.func()

When you run this it will print Hello World.

Note :

Remember this. You can import the module if you are in the same directory that you have created the so file. Other wise it cannot be imported. To import module in python file while you are in different directory you have to copy the .py file, .so file and .pyc file to that directory. Then only you can run the program that you create using the module.

No comments:

Post a Comment