How to execute a python script in .NET application

Gerard Flanagan grflanagan at yahoo.co.uk
Sat Oct 7 11:46:31 CEST 2006


Chandra wrote:

> Hi,
>
> Is there a way to execute a python script(file) in ASP.NET application
> (programmatically)??
>
> Regards,
> Chandra


I thought IIS would prevent this, but the following works for me at
home (ASP.NET 1.1). A production setup may be a different matter.

using System.Diagnostics

	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;

		private void Page_Load(object sender, System.EventArgs e)
		{
			ProcessStartInfo startInfo;
			Process process;
			string directory = @"c:\python\python24\Lib\site-packages";
			string script = "test.py";

			startInfo = new ProcessStartInfo("python");
			startInfo.WorkingDirectory = directory;
			startInfo.Arguments = script;
			startInfo.UseShellExecute = false;
			startInfo.CreateNoWindow = true;
			startInfo.RedirectStandardOutput = true;
			startInfo.RedirectStandardError = true;

			process = new Process();
			process.StartInfo = startInfo;
			process.Start();

			string s;
			while ((s = process.StandardOutput.ReadLine()) != null)
			{
				Label1.Text += s;
			} 
		}
}




More information about the Python-list mailing list