Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ASP.NET MVC调用文件.py中的外部python函数_Python_Asp.net Mvc - Fatal编程技术网

使用ASP.NET MVC调用文件.py中的外部python函数

使用ASP.NET MVC调用文件.py中的外部python函数,python,asp.net-mvc,Python,Asp.net Mvc,在ASP.NETMVC应用程序中,我有一个XML文件。我还有一个python函数execute(xml_文件),它将这个xml文件作为test.py(用python3编写)中的输入。此函数将执行此xml文件并为我返回结果列表。我希望ASP.NET将获取该结果并显示。如何在ASP.NET MVC中调用该外部模块?您可以在C#code上启动一个新的进程来运行python并运行脚本 您可以启动一个调用python的新进程。请参见带有注释的示例: ProcessStartInfo start = new

在ASP.NETMVC应用程序中,我有一个XML文件。我还有一个python函数execute(xml_文件),它将这个xml文件作为test.py(用python3编写)中的输入。此函数将执行此xml文件并为我返回结果列表。我希望ASP.NET将获取该结果并显示。如何在ASP.NET MVC中调用该外部模块?

您可以在C#code上启动一个新的
进程来运行python并运行脚本

您可以启动一个调用python的新进程。请参见带有注释的示例:

ProcessStartInfo start = new ProcessStartInfo();

// full path of python exe
start.FileName = "c:\\Python\\Python.exe";

string cmd = "C:\\scripts\\test.py";
string args = "";

// define the script with arguments (if you need them).
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);

// Do not use OS shell
start.UseShellExecute = false;

// You do not need new window 
start.CreateNoWindow = true; 

// Any output, generated by application will be redirected back
start.RedirectStandardOutput = true;

// Any error in standard output will be redirected back (for example exceptions)
start.RedirectStandardError = true; 

// start the process
using (Process process = Process.Start(start))
{
    using (StreamReader reader = process.StandardOutput)
    {
        // Here are the exceptions from our Python script
        string stderr = process.StandardError.ReadToEnd(); 

        // Here is the result of StdOut(for example: print "test")
        string result = reader.ReadToEnd(); 

        return result;
    }
}

脚本test.py中有几个函数,但我只想调用execute(xml_文件),我怎么做?