Вызов внешних функций Python в файле .py с использованием ASP .NET MVC - PullRequest
0 голосов
/ 19 мая 2018

В приложении ASP.NET MVC у меня есть файл XML.У меня также есть функция Python execute (xml_file), которая принимает этот XML-файл в качестве входных данных в test.py (написанном на python3).Эта функция выполнит этот XML-файл и вернет мне список результатов.И я хочу, чтобы ASP .NET взял этот результат и отобразил.Как я могу назвать этот внешний модуль в ASP .NET MVC?

1 Ответ

0 голосов
/ 19 мая 2018

Вы можете запустить новый Process в коде C # для запуска 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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...