C Python.h API OpenSSL 导入错误
C Python.h API ImportError with 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, '[=12=]', 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
和 -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
构建模块的工作 around/alternative 方法,并且仍然在 python3
中使用它
gcc -fPIC -shared hashmodule.c -o hashmodule.so -I /usr/include/python3.8 -lcrypto
模块工作正常。
我把这个答案留给其他人in-case他们有同样的问题。
编辑:
还刚刚找到名为“extra_link_args”的 setup.py 扩展 arg“
将其添加到您的 distutils.core.Extension()
参数中并像往常一样使用 python3 setup.py build
& python3 setup.py install
进行编译
我正在通过使用 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, '[=12=]', 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
和 -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
构建模块的工作 around/alternative 方法,并且仍然在 python3
gcc -fPIC -shared hashmodule.c -o hashmodule.so -I /usr/include/python3.8 -lcrypto
模块工作正常。
我把这个答案留给其他人in-case他们有同样的问题。
编辑:
还刚刚找到名为“extra_link_args”的 setup.py 扩展 arg“
将其添加到您的 distutils.core.Extension()
参数中并像往常一样使用 python3 setup.py build
& python3 setup.py install