1

I have a python script called web_interface.py

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://xxxxxx.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():
  ReadGridData()

if __name__ == "__main__":
  main()

Trying to call this script from c++ program as

  Py_Initialize();
  pName = PyUnicode_DecodeFSDefault("web_interface");
  pModule = PyImport_Import(pName);
  Py_DECREF(pName);
  if (pModule != NULL) {
    pFunc = PyObject_GetAttrString(pModule, "ReadDataTest");
    if (pFunc && PyCallable_Check(pFunc)) {
      pString=PyObject_CallObject(pFunc, NULL);
      if(pString != NULL) {
         printf("Result of call: %s\n", pString);
         Py_DECREF(pString);
      }
    }
    Py_XDECREF(pFunc);
    Py_DECREF(pModule);
  }else{
    printf("Can't call python \n");
  }
  Py_FinalizeEx();

Always have "Can't call python \n"

What could be wrong?

EDIT: The place called the python script is inside a timer callback function. When export $PYTHONPATH to the folder where python script is located, the error is

Can't call python 
Exception ignored in: <module 'threading' from '/usr/lib/python3.6/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 1289, in _shutdown
    assert tlock.locked()
SystemError: <built-in method locked of _thread.lock object at 0x7f77a1b580> returned a result with an error set
batuman
  • 7,066
  • 26
  • 107
  • 229
  • Does it make any difference if you replace `pModule = PyImport_Import(pName);` with `pModule = PyImport_Import("web_interface");` ? Also, are you certain that web_interface.py is located in a directory that is part of your $PYTHONPATH ? – Jeremy Friesner Aug 24 '20 at 04:38
  • @JeremyFriesner Replaced and error is `error: cannot convert ‘const char*’ to ‘PyObject* {aka _object*}’ for argument ‘1’ to ‘PyObject* PyImport_Import(PyObject*)’` so it is necessary. I did `export PYTHONPATH=/opt/nvidia/deepstream/` to the folder where python script is located. But still have error. – batuman Aug 24 '20 at 05:18
  • @JeremyFriesner Hi I have added EDIT. When I export $PYTHONPATH, that error came out. – batuman Aug 24 '20 at 05:35

0 Answers0