简体   繁体   English

Python脚本和Win32程序之间的通信(在Delphi中开发)

[英]Communication between Python script and a win32 program (developed in Delphi)

I'm new to Python and I want my Python script to be able to communicate with my Windows program (developed in Delphi, FWIW). 我是Python的新手,我希望Python脚本能够与Windows程序(在FWIW的Delphi中开发)进行通信。

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. 基本上,Python脚本将从其他数据源下载一组数据,对于下载的每条记录,我都会记录下来并告诉Windows程序。 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? 对于日志记录,如果可能的话,我将使用Python标准日志记录库,但是告诉win32程序最简单的方法是什么,以便向最终用户显示下载过程?

Note: I know Python4Delphi , but it's not documented well, and I want to keep things simple. 注意:我知道Python4Delphi ,但是没有很好的文档记录,我想保持简单。

Edit 1: There will be only one Delphi exe and multiple python scripts. 编辑1:将只有一个Delphi exe和多个python脚本。

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 . 如果您的Delphi程序是执行Python程序的程序,则只需让脚本将进度消息写入标准输出 ,然后就可以在Delphi程序中读取它们 (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.) (如果以这种方式进行操作,那么一个程序是用Python编写的,另一个程序是用Delphi编写的就没关系。任何一个程序都可以用您想要的任何语言编写。)

You could use a named pipe or a socket to communicate between the Python code and the Delphi code. 您可以使用命名管道或套接字在Python代码和Delphi代码之间进行通信。 For interfacing Python to named pipes you could could use ctypes (example here ). 为了将Python连接到命名管道,您可以使用ctypes( 此处为示例)。

Altermatively, you could create a COM component in Delphi and make calls to it from Python (ActivePython includes all the Windows bits you need). 或者,您可以在Delphi中创建一个COM组件,然后从Python对其进行调用(ActivePython包括您需要的所有Windows位)。

My own idea: 我自己的想法:

Maybe call PostMessage(WM_CopyData) in the Python script? 也许在Python脚本中调用PostMessage(WM_CopyData) But what's the best/standard way of calling that win32 API in Python? 但是用Python调用Win32 API的最佳/标准方式是什么?

Use PyWin32 package: http://pypi.python.org/pypi/pywin32 It provides you with access to the whole WinAPI. 使用PyWin32软件包: http : //pypi.python.org/pypi/pywin32它为您提供了访问整个WinAPI的权限。

You could use for example COM: http://docs.activestate.com/activepython/2.7/pywin32/html/com/win32com/HTML/docindex.html 您可以使用例如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. 现在,我可以让他们在同一台计算机上交谈,也可以在局域网或Internet上交谈。 Easy and fun. 简单而有趣。

In python, socket programming library options abound, as they do in Delphi. 在python中,套接字编程库选项很多,就像在Delphi中一样。 Indy is the most common solution in Delphi. Indy是Delphi中最常见的解决方案。 Simple socket servers that are quite robust enough for you to use, are also built into python. 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? 另外,由于Python可移植到Windows之外,您是否真的要限制自己使用命名管道和COM,并将Python程序永远链接到Windows?

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. 但是,如果您的脚本是Delphi程序的附件,那么Python4Delphi(调用Python函数并使用它们的结果的功能)正是您所需要的,而且据我所知,它没有很好的文档记录,因为它易于使用,不必如此。 How much documentation did you read about WriteLn before you did your first WriteLn('HelloWorld') ? 在进行第一个WriteLn('HelloWorld')之前,您阅读了多少有关WriteLn的文档?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM