首页 > 解决方案 > C Python.h API ImportError 与 OpenSSL

问题描述

我正在通过使用 OpenSSL 和 MD5 制作散列示例来尝试 C 的 Python API (python3),但是在尝试在 python3 中导入模块时出现错误。这是我的 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 设置脚本

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 添加了-lssland标志,并且当我运行and-lcrypto时构建和安装编译很好。这是输出:python3 setup.py buildpython3 setup.py install

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
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 并执行以下命令时,会出现此错误:

>>> 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 函数是否包含在显示以下内容的模块中:

                 U MD5_Final
                 U MD5_Init
                 U MD5_Update

任何帮助将不胜感激,如果我遗漏了什么,请告诉我。

非常感谢!

标签: cpython-3.xapimoduleopenssl

解决方案


几天后,我设法找到了一种解决方法/替代方法来构建模块而不使用它,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 buildpython3 setup.py install


推荐阅读