简体   繁体   English

通过VB.Net在远程服务器上执行python脚本并将结果返回到本地计算机

[英]Execute python script on remote server via VB.Net and return result to local machine

The problem is the following: My local machine (computer A) runs VB.Net code locally. 问题如下:我的本地计算机(计算机A)在本地运行VB.Net代码。 I need to run a Python script on another remote machine (computer B, the Python script resides on computer B) and get the result back to computer A , preferrably without having the Python script on computer B writing the output to a .csv file (or similar) and computer A then reading that file. 我需要在另一台远程计算机(计算机B,Python脚本驻留在计算机B上)上运行Python脚本,并将结果返回计算机A, 最好不要让计算机B上的Python脚本将输出写入.csv文件(或类似的文件),然后计算机A读取该文件。 That is, I would like to accomplish pretty much the following: 也就是说,我想完成以下工作:

(VB.Net code on computer A) (计算机A上的VB.Net代码)

matrixResult = getMatrixFromPython(args)

where the function getMatrixFromPython "invokes" the Python script on computer B and returns the matrix that script produces. 其中,函数getMatrixFromPython调用计算机B上的Python脚本,并返回该脚本生成的矩阵。 My current solution is using WMI to execute a .bat file on the remote machine, and that .bat file in turn runs the Python script. 我当前的解决方案是使用WMI在远程计算机上执行.bat文件,然后该.bat文件又运行Python脚本。 This, however, leaves the problem of getting the result back to the VB.Net code on computer A. 但是,这留下了将结果返回到计算机A上的VB.Net代码的问题。

Any help greatly appreciated. 任何帮助,不胜感激。 Thank you. 谢谢。

You could do this a number of ways. 您可以通过多种方式执行此操作。

  1. You could implement a WCF service in VB.net that executes the python script and passes the result back to the caller 您可以在VB.net中实现WCF服务,该服务执行python脚本并将结果传递回调用方

    a. 一种。 The python execution might also be easier to interact with if you use ironpython to run it directly rather than relying on capturing output from process.start and then interpreting it back into a data structure (although if you had to go that route then I'd suggest using JSON as there are libraries in .net and python that will read/write this format) 如果您使用ironpython直接运行它,而不是依赖于从process.start捕获输出然后将其解释回数据结构,则与python执行交互可能也更容易(尽管如果您必须走那条路线,那么我会建议使用JSON,因为.net和python中有可以读取/写入此格式的库)

  2. Use powershell remoting to perform the remote execution, this will make it much simpler than with wmi to capture the output. 使用powershell远程执行远程执行,这比使用wmi捕获输出要简单得多。 Issues with this might be that it's not installed/configured everywhere it needs to be 问题在于可能没有在需要安装的任何地方安装/配置它

  3. Convert the python script to run as a CGI script and run it from a web server. 将python脚本转换为作为CGI脚本运行,然后从Web服务器运行它。 This means that calling it and getting outputis as simple as fetching a URL 这意味着调用它并获取输出就像获取URL一样简单

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

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