How to run a .py (Python) file in ASP.NET?
I have a GetList.py
file which consumes Web Service and saves the out开发者_开发问答put in XML on the server.
How do I invoke GetList.py
so it saves the output in XML on the server before displaying the XML output in .ASPX
page?
You can create one batch file which contains call to python file and call that batch file from you .net application. To call batch file, you can use Process class. For example, suppose you have test.py file containing following code :
print "hello world"
then create one batch file (file having .bat extension) which has following contents :
python C:\test.py
Assuming you are using C#, and ur batchfile is stored in (C:\test.bat) you can use following code to invoke batch file
Process.Start("C:\test.bat");
You can have more details about Process class here
If your server has a Python interpreter installed, use that. (It's usually in /usr/bin/python)
If it doesn't (and it probably doesn't, since you use .NET), use IronPython. It's based on .NET and works very nicely with ASP.NET. Fair warning: if your GetList.py script uses parts of the CPython standard library that haven't been implemented in IronPython, you might have to change the script. See this article to get a basic intro to IronPython and see how it fits in with .NET.
精彩评论