Return 字符串从 python 到 C++
Return string from python to C++
我的 python 代码包含 returning 字符串的方法。
import urllib.request
import ssl
import suds.transport.http
from suds.client import Client
class UnverifiedHttpsTransport(suds.transport.http.HttpTransport):
def __init__(self, *args, **kwargs):
super(UnverifiedHttpsTransport, self).__init__(*args, **kwargs)
def u2handlers(self):
handlers = super(UnverifiedHttpsTransport, self).u2handlers()
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
handlers.append(urllib.request.HTTPSHandler(context=context))
return handlers
url="https://atictest.com/datamanagement.asmx?WSDL"
client = Client(url, transport=UnverifiedHttpsTransport())
def ReadDataTest():
result = client.service.ReadTestData()
return result
def ReadGridData():
result = client.service.ReadGridData()
return result
def main():
result=ReadGridData()
print(result)
if __name__ == "__main__":
main()
如果调用 ReadDataTest() 方法结果有字符串 {"Message":"You Have Successfully Connected"}
.
由于这个 python 方法是从 C++ 调用的,我需要在 C++ 中解析字符串 return。
我试过
pFunc_readtest = PyObject_GetAttrString(pModule, "ReadDataTest");
if (pFunc_readtest && PyCallable_Check(pFunc_readtest)) {
pValue = PyObject_CallObject(pFunc_readtest, NULL);
if(pValue != NULL) {
std::string m_gettextFunction = PyObject_GetAttrString(pValue, "gettext");
printf("Result of call: %c\n", m_gettextFunction);
Py_DECREF(pValue);
}
}
但是编译出错。如何从 python 接收字符串到 C++?
PyObject_GetAttrString
returns一个PyObject *
,妥善处理,这里是代码:
pFunc_readtest = PyObject_GetAttrString(pModule, "ReadDataTest");
if (pFunc_readtest && PyCallable_Check(pFunc_readtest)) {
pValue = PyObject_CallObject(pFunc_readtest, NULL);
if(pValue != NULL) {
PyObject * res = PyObject_GetAttrString(pValue, "gettext");
if (!PyUnicode_Check(res)) {
// not a string, return error here
}
std::string m_gettextFunction = std::string(PyUnicode_AsUTF8(res));
printf("Result of call: %c\n", m_gettextFunction);
Py_DECREF(pValue);
}
}
如果 gettext
是一个方法然后调用它,不要只是获取它:
PyObject * res = PyObject_CallMethod(pValue, "gettext", NULL);
我的 python 代码包含 returning 字符串的方法。
import urllib.request
import ssl
import suds.transport.http
from suds.client import Client
class UnverifiedHttpsTransport(suds.transport.http.HttpTransport):
def __init__(self, *args, **kwargs):
super(UnverifiedHttpsTransport, self).__init__(*args, **kwargs)
def u2handlers(self):
handlers = super(UnverifiedHttpsTransport, self).u2handlers()
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
handlers.append(urllib.request.HTTPSHandler(context=context))
return handlers
url="https://atictest.com/datamanagement.asmx?WSDL"
client = Client(url, transport=UnverifiedHttpsTransport())
def ReadDataTest():
result = client.service.ReadTestData()
return result
def ReadGridData():
result = client.service.ReadGridData()
return result
def main():
result=ReadGridData()
print(result)
if __name__ == "__main__":
main()
如果调用 ReadDataTest() 方法结果有字符串 {"Message":"You Have Successfully Connected"}
.
由于这个 python 方法是从 C++ 调用的,我需要在 C++ 中解析字符串 return。
我试过
pFunc_readtest = PyObject_GetAttrString(pModule, "ReadDataTest");
if (pFunc_readtest && PyCallable_Check(pFunc_readtest)) {
pValue = PyObject_CallObject(pFunc_readtest, NULL);
if(pValue != NULL) {
std::string m_gettextFunction = PyObject_GetAttrString(pValue, "gettext");
printf("Result of call: %c\n", m_gettextFunction);
Py_DECREF(pValue);
}
}
但是编译出错。如何从 python 接收字符串到 C++?
PyObject_GetAttrString
returns一个PyObject *
,妥善处理,这里是代码:
pFunc_readtest = PyObject_GetAttrString(pModule, "ReadDataTest");
if (pFunc_readtest && PyCallable_Check(pFunc_readtest)) {
pValue = PyObject_CallObject(pFunc_readtest, NULL);
if(pValue != NULL) {
PyObject * res = PyObject_GetAttrString(pValue, "gettext");
if (!PyUnicode_Check(res)) {
// not a string, return error here
}
std::string m_gettextFunction = std::string(PyUnicode_AsUTF8(res));
printf("Result of call: %c\n", m_gettextFunction);
Py_DECREF(pValue);
}
}
如果 gettext
是一个方法然后调用它,不要只是获取它:
PyObject * res = PyObject_CallMethod(pValue, "gettext", NULL);