C 代码尝试通过 xmlrpc 进行 Python 远程过程调用时崩溃

发布于 2024-10-28 20:48:51 字数 1206 浏览 1 评论 0原文

我正在尝试创建 C 代码来创建 Python xmlrpc 客户端并调用 xmlrpc 服务器上的方法(我正在考虑将其用作挂钩 DLL 的 IPC)。

这是代码......在它起作用之前我不会分层引用计数。

#include <Python.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

static PyObject *xmlrpc_server_proxy = NULL;
static PyObject *set_server_proxy(void);
static void say_hi(void);

int main()
{
    xmlrpc_server_proxy = set_server_proxy();
    say_hi();
    return 0;
}

static PyObject *
set_server_proxy()
{
        PyObject *xmlrpc_client_mod, *xmlrpc_server_proxy_class, *location, *args;
        PyObject *result;
        Py_Initialize();
        xmlrpc_client_mod = PyImport_ImportModule("xmlrpc.client");
        xmlrpc_server_proxy_class = PyObject_GetAttrString(xmlrpc_client_mod, "ServerProxy");
        location = PyUnicode_FromString("http://127.0.0.1:8000/");
        args = Py_BuildValue("(O)", location);
        result = PyObject_CallObject(xmlrpc_server_proxy_class, args);
        Py_Finalize();
        return result;
}

static void say_hi()
{
    PyObject_CallMethod(xmlrpc_server_proxy, "say_hi", "()");
}

我已经确认我的 Python xmlrpc 服务器在从另一个 Python 服务器代理调用时工作正常。当我尝试运行上述可执行文件时,它在 PyObject_CallMethod() 上崩溃。为什么?

I'm trying to create C code that creates an Python xmlrpc client and calls methods on the xmlrpc server (I'm thinking of using this as IPC for a hook DLL).

Here's the code ... I'm not going to layer in reference counting until it works.

#include <Python.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

static PyObject *xmlrpc_server_proxy = NULL;
static PyObject *set_server_proxy(void);
static void say_hi(void);

int main()
{
    xmlrpc_server_proxy = set_server_proxy();
    say_hi();
    return 0;
}

static PyObject *
set_server_proxy()
{
        PyObject *xmlrpc_client_mod, *xmlrpc_server_proxy_class, *location, *args;
        PyObject *result;
        Py_Initialize();
        xmlrpc_client_mod = PyImport_ImportModule("xmlrpc.client");
        xmlrpc_server_proxy_class = PyObject_GetAttrString(xmlrpc_client_mod, "ServerProxy");
        location = PyUnicode_FromString("http://127.0.0.1:8000/");
        args = Py_BuildValue("(O)", location);
        result = PyObject_CallObject(xmlrpc_server_proxy_class, args);
        Py_Finalize();
        return result;
}

static void say_hi()
{
    PyObject_CallMethod(xmlrpc_server_proxy, "say_hi", "()");
}

I've confirmed that my Python xmlrpc server works fine when called from another Python server proxy. When I try to run the above executable, it crashes on the PyObject_CallMethod(). Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2024-11-04 20:48:51

set_server_proxy() 末尾处,您调用 Py_Finalize() ,它会销毁解释器,随后您调用 say_hi() ,它假定翻译仍然存在。当 Python 解释器代码尝试引发错误时,PyErr_Occurred() 函数会获取指向当前线程状态的指针,该指针为 NULL;它取消引用它,这会产生段错误。

将解释器初始化调用移至 main() 函数内:

int main()
{
    Py_Initialize();
    xmlrpc_server_proxy = set_server_proxy();
    say_hi();
    Py_Finalize();
    return 0;
}

其次,如果您尝试使用 Python 的标准 xmlrpclib.ServerProxy,您可能需要将导入更改为:

xmlrpc_client_mod = PyImport_ImportModule("xmlrpclib");

Near the end of set_server_proxy() you are calling Py_Finalize() which destroys the interpreter, and subsequently you are calling say_hi() which assumes the interpreter still exists. When the Python interprer code attempts to raise an error, the PyErr_Occurred() function gets a pointer to the current thread state, which is NULL; it dereferences it and this generates the segfault.

Move your interpreter initialization calls inside the main() function:

int main()
{
    Py_Initialize();
    xmlrpc_server_proxy = set_server_proxy();
    say_hi();
    Py_Finalize();
    return 0;
}

Secondarily, if you are trying to use Python's standard xmlrpclib.ServerProxy you may need to change your import to:

xmlrpc_client_mod = PyImport_ImportModule("xmlrpclib");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文