C 代码尝试通过 xmlrpc 进行 Python 远程过程调用时崩溃
我正在尝试创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
set_server_proxy()
末尾处,您调用Py_Finalize()
,它会销毁解释器,随后您调用say_hi()
,它假定翻译仍然存在。当 Python 解释器代码尝试引发错误时,PyErr_Occurred()
函数会获取指向当前线程状态的指针,该指针为NULL
;它取消引用它,这会产生段错误。将解释器初始化调用移至
main()
函数内:其次,如果您尝试使用 Python 的标准
xmlrpclib.ServerProxy
,您可能需要将导入更改为:Near the end of
set_server_proxy()
you are callingPy_Finalize()
which destroys the interpreter, and subsequently you are callingsay_hi()
which assumes the interpreter still exists. When the Python interprer code attempts to raise an error, thePyErr_Occurred()
function gets a pointer to the current thread state, which isNULL
; it dereferences it and this generates the segfault.Move your interpreter initialization calls inside the
main()
function:Secondarily, if you are trying to use Python's standard
xmlrpclib.ServerProxy
you may need to change your import to: