I'm new to Python and I want my Python script to be able to communicate with my Windows program (developed in Delphi, FWIW).
Basically, the Python script will download a set of data from other data sources, and for each record it's downloaded, I'll log it and tell the Windows program. For logging I'll use the Python standard logging library if possible, but what's the easiest way to tell the win32 program so that I can show the process of the downloading to the end user?
Note: I know Python4Delphi, but it开发者_JS百科's not documented well, and I want to keep things simple.
Edit 1: There will be only one Delphi exe and multiple python scripts.
Thanks.
If your Delphi program is the one executing the Python program, then you can simply have the script write progress messages to standard output, and you can read them in your Delphi program. (If you do it this way, then it doesn't matter that one program is in Python and the other is in Delphi. Either program can be written in whatever language you want.)
You could use a named pipe or a socket to communicate between the Python code and the Delphi code. For interfacing Python to named pipes you could could use ctypes (example here).
Altermatively, you could create a COM component in Delphi and make calls to it from Python (ActivePython includes all the Windows bits you need).
My own idea:
Maybe call
PostMessage(WM_CopyData)
in the Python script? But what's the best/standard way of calling that win32 API in Python?
Use PyWin32 package: http://pypi.python.org/pypi/pywin32 It provides you with access to the whole WinAPI.
You could use for example COM: http://docs.activestate.com/activepython/2.7/pywin32/html/com/win32com/HTML/docindex.html
I would make a simple socket server in one side, and a simple socket client on the other one. Now I can have them talking on the same computer, and also, talking across a local area network, or across the internet. Easy and fun.
In python, socket programming library options abound, as they do in Delphi. Indy is the most common solution in Delphi. Simple socket servers that are quite robust enough for you to use, are also built into python.
Also, since Python is portable beyond windows, do you really want to limit yourself to named pipes and COM and chain your Python program to windows forever?
If however, your scripts are an adjunct to the Delphi program, then Python4Delphi (ability to call Python functions and consume their results) is exactly what you need, and as far as I know, it's not well documented because it's so easy to use, it doesn't need to be. How much documentation did you read about WriteLn before you did your first WriteLn('HelloWorld')
?
精彩评论