Python Executing Existing (and Large) C ++ Code

I have a C ++ program that uses the cryptopp library to decrypt / encrypt messages.

It offers two interface methods, encrypt and decrypt , that receive a string and work with it using cryptopp methods.

Is there a way to use both methods in Python without manually packing all cryptopp and files?

Example:

 import cppEncryptDecrypt string foo="testing" result = encrypt(foo) print "Encrypted string:",result 
+1
source share
2 answers

If you can make a DLL from this C ++ code by exposing these two methods (ideally like "extern C", which makes all matching tasks a lot easier), ctypes might be the answer without requiring a third-party tool or extension. Otherwise, it's your choice between cython , the good old SWIG , SIP , Boost , ... - many, many of the third-party tools will allow your Python code to call these two C ++ entry points without having to wrap anything else besides them.

+6
source

As Alex said, you can create a dll, export the function you want to access with python with, and use ctypes ( http://docs.python.org/library/ctypes.html ) to access, for example

 >>> libc = cdll.LoadLibrary("libc.so.6") >>> printf = libc.printf >>> printf("Hello, %s\n", "World!") Hello, World 

or there is an alternative simpler approach that many people do not consider, but are equally useful in many cases, that is, they directly call the program from the command line. You said that you already have a working program, so I assume that it encrypts and decrypts from the command line? if so, why don't you just call the program from the os.system or subprocess module instead of delving into the code and modifying it and maintaining it.

I would say to go the second way if he cannot fulfill your requirements.

+4
source

Source: https://habr.com/ru/post/1387962/


All Articles