c - C Python.h API ImportError 与 OpenSSL
问题描述
我正在通过使用 OpenSSL 和 MD5 制作散列示例来尝试 C 的 Python API (python3),但是在尝试在 python3 中导入模块时出现错误。这是我的 C 源代码:
hashmodule.c
#include <stdlib.h>
#include <string.h>
#include <Python.h>
#include <openssl/md5.h>
unsigned char * __c_md5hash(const /*unsigned*/ char * string) {
unsigned char * raw_hash = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
memset(raw_hash, '\0', MD5_DIGEST_LENGTH);
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, string, strlen(string));
MD5_Final(raw_hash, &ctx);
return raw_hash;
}
static PyObject * md5hash(PyObject * self, PyObject * args) {;
const /*unsigned*/ char * string;
if (!PyArg_ParseTuple(args, "s", &string))
return NULL;
return Py_BuildValue("s", __c_md5hash(string));
}
static PyMethodDef hashmethods[] = {
{"md5hash", md5hash, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef hashmodule = {
PyModuleDef_HEAD_INIT,
"hashmodule",
"a simple test hashing module for python3",
-1,
hashmethods
};
PyMODINIT_FUNC PyInit_testmodule(void) {
return PyModule_Create(&hashmodule);
}
这是我的 python3 设置脚本
setup.py
from distutils.core import setup, Extension
import sysconfig
setup(
name="hashmodule",
version="1.0",
ext_modules=[
Extension(
"hashmodule",
sources=["hashmodule.c"],
extra_compile_args=["-lcrypto", "-lssl"]
)
]
)
如setup.py
文件所示,我已经为 openssl 添加了-lssl
and标志,并且当我运行and-lcrypto
时构建和安装编译很好。这是输出:python3 setup.py build
python3 setup.py install
python3 setup.py build
running build
running build_ext
building 'hashmodule' extension
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-s
trong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -
Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.8 -c hashmodule.c -o build/temp.linux-x86_64-3.8/hashmod
ule.o -lcrypto -lssl
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-z,relro -g -
fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.l
inux-x86_64-3.8/hashmodule.o -o build/lib.linux-x86_64-3.8/hashmodule.cpython-38-x86_64-linux-gnu.so
python3 setup.py install
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-3.8/hashmodule.cpython-38-x86_64-linux-gnu.so -> /usr/local/lib/python3.8/dist-packag
es
running install_egg_info
Writing /usr/local/lib/python3.8/dist-packages/hashmodule-1.0.egg-info
如构建中所示,它包含使用 openssl 进行正常编译所需的正确加密和 ssl 标志。但是,当我然后进入 python3 并执行以下命令时,会出现此错误:
python3
>>> import hashmodule
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.8/dist-packages/hashmodule.cpython-38-x86_64-linux-gnu.so: undefined symbol: MD5_Final
>>>
我跑去nm
查看 MD5 函数是否包含在显示以下内容的模块中:
nm -S /usr/local/lib/python3.8/dist-packages/hashmodule.cpython-38-x86_64-linux-gnu.so | grep MD5
U MD5_Final
U MD5_Init
U MD5_Update
任何帮助将不胜感激,如果我遗漏了什么,请告诉我。
非常感谢!
解决方案
几天后,我设法找到了一种解决方法/替代方法来构建模块而不使用它,setup.py
并且仍然在 python3 中使用它
gcc -fPIC -shared hashmodule.c -o hashmodule.so -I /usr/include/python3.8 -lcrypto
并且模块工作正常。
我把这个答案留给其他人,以防他们有同样的问题。
编辑:
也刚刚找到名为“extra_link_args”的 setup.py 扩展 arg
将其添加到您的参数中并像往常一样使用&distutils.core.Extension()
编译python3 setup.py build
python3 setup.py install
推荐阅读
- image - 如何根据matlab中另一个坐标矩阵中点之间的距离对坐标矩阵进行排序?
- reactjs - 在复合组件中使用类似样式的属性进行样式设置
- python - 尝试使用时间戳作为位置在 pandas 中选择数据切片
- eigen - Catapult HLS 工具中是否支持 Eigen C++ 模板库?
- xcode - 如何找到多视图控制器的编辑器
- html - 如何显示html div,以便在表单提交后自动显示错误消息
- emacs - 如何根据起始字符设置emacs线条颜色
- intellij-idea - SonarQube v5.6.6 Rest API - 权限不足
- selenium - 如何发现在聚合物测试期间使用了哪个 selenium 和 chromedriver
- javascript - 错误:Firebase 的 Cloud Functions 生成 EACCES(ghostscript)