准备使用 C extensions编译的C文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pcap.h>
#include <Python.h>
static PyObject *
demo_send(PyObject *self, PyObject *args)
{
const char* packet;
const int length;
// convert PyObject to C values
if (!PyArg_ParseTuple(args, "si", &packet, &length))
return NULL;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
dev = pcap_lookupdev(errbuf);
if(dev == NULL) {
fprintf(stderr,"%s\n",errbuf);
exit(1);
}
descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
if(descr == NULL) {
printf("pcap_open_live(): %s\n",errbuf);
exit(1);
}
int sts = pcap_sendpacket(descr, packet, length);
return Py_BuildValue("i", sts);;
}
// module's method table
static PyMethodDef DemoMethods[] = {
{"send", demo_send, METH_VARARGS, "Send packets"},
{NULL, NULL, 0, NULL}
};
// module’s initialization function
PyMODINIT_FUNC
initdemo(void)
{
(void)Py_InitModule("Csend", DemoMethods);
}
以及setup.py文件
from distutils.core import setup, Extension
module1 = Extension('Csend',
sources = ['release.c']
)
setup (name = 'a demo extension module',
version = '1.0',
description = 'send packets',
ext_modules = [module1])
但是编译后结果出现了
"ImportError: ./Csend.so: undefined symbol: pcap_sendpacket"
的错误。
我想起来在gcc编译地过程中需要加入-lpcap的编译选项,所以在终端又尝试了一次,但一样的报错结果。
我想使用C来加速python项目的发包速度,结果使用C extension在编译时出现了问题,如果还有什么其他发包方式的选择,也可以请教一下,谢谢!
待修正的地方
PyMODINIT_FUNC 函数名必须是 init<模块名>. 假设模块名称为 csend, 那么
这个模块名称必须与 setup.py 中的一致.
c 模块的外部依赖库应该写入 setup.py, 如
使用
python setup.py install
安装模块, 或打包后发到别处安装.如源码方式打包
或二进制方式打包
其他发包方法