asp.net 网站调用python执行返回信息Demo

首先需要引入IronPython,可以通过NuGet搜索获得,基于4.5以上框架集

using System;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
public partial class python : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        RunPythonShell();
    }
    /// <summary>
    /// 调用Python
    /// </summary>
    private void RunPythonShell()
    {
        ScriptRuntime pyRuntime = Python.CreateRuntime();
        //python文件绝对路径
        string path = string.Format(@"{0}1.py", Server.MapPath("./"));
        dynamic py = pyRuntime.UseFile(path);
        //调用Python 的函数run()
        Response.Write(py.show());
    }

}

python文件代码  1.py

def show ():
    return "hello world!"

 

posted @ 2020-10-14 11:35  uxinxin  阅读(894)  评论(0编辑  收藏  举报