I've got a background in PHP, dotNet and am charmed by Python. I want to transpose functionality from PHP to Python step by step, running bits and pieces side-by-side. During this transition, which could take 2 years since the app is enormous, I am bound to IIS. I've got 15 years background of web-programming, including some C work in an ISAPI module on IIS which is the kind of work I don't want to dive into any more.
It seems Python just doesn't run well on IIS. I've struggled with FastCGI (not supported, just for PHP) and PyIsapie
(badly documented, couldn't get it up and running). In the end I got it up and running with a HeliconZoo dll BUT:
My next problem is: how to debug/develop a site? In PHP you install a debugger and whenever you have a problem in your website, you just debug it, set a breakpoint, step through code, inspect watches and such. It seems to me this is the most rudimentary type of work for a developer or troubleshooter. I've bought WingIDE which is an excellent tool and debugger but it can't hook into the Python instance in the IIS process for some reason so no debugging. I noticed Helicon starts Python with -O
so I even recompiled Python to ignore this flag altogether but my debugger (WingIDE) just won't come up.
I can set up a PHP 'hello world' website on IIS in half an hour including download time. I think I've spent about 120 hours or more getting this to work for Python to no avail. I've bought Programming Python and Learning Python which is about 3000 pages. And I've googled until I dropped.
I think Python is a great language but I'm on the verge of aborting my attempts. Is there anyone who can give me a step-by-step instruction on how to set this up on IIS7?
To run a flask application in IIS server, we need the “flask” and “wfastcgi” libraries. We can install the Python libraries by executing the “pip” command in CMD. The Flask application connects with the FastCGI module of the server to expose the micro-service APIs.
I just did this in 5 minutes.
Insure you have IIS. run: %windir%\system32\OptionalFeatures.exe
. Or, via pointy-clicky: Start...Control Panel...Programs and Features... (and then on the left hand side) Turn Windows Features on or Off. Make sure CGI is installed, under the IIS node.
Download Python for Windows, from python.org . I grabbed Python2.7. Make sure you get the x64 version if you have an x64 version of Windows.
Unpack and install that python MSI. Choose the default, which puts python into c:\Python27
Create a directory to hold your "development" python scripts. Eg, c:\dev\python
Set the permissions on the files in the directory c:\dev\python
to allow IIS to read and execute. Do this by running these two icacls.exe commands from the command line:
cd \dev\python
icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"
Open IIS manager. Run %windir%\system32\inetsrv\iis.msc
, or do this via the control panel: Start...Control Panel...Administrative Tools...Internet Information Services (IIS) Manager. Create a new application. Specify the virtual path as /py
and the physical path as c:\dev\python
.
Within that IIS application, add a script map for *.py
, and map it to c:\python27\python.exe %s %s
create a "HelloWorld.py" file in c:\dev\python
with this as the content:
print('Content-Type: text/plain')
print('')
print('Hello, world!')
invoke http://localhost/py/helloworld.py
just make sure the path to the directory holding the cgi scripts doesn't have spaces or &.
i tried lots of things for many days and nothing worked then i changed the path and it worked
UPDATE:
If it has spaces, put quotes around the path, but not the %s %s
like this:
"C:\Program Files\Python36\python.exe" %s %s
Since this is quite an old question with some old answers (no accepted answer), here is a more up to date approach. The Microsoft doc below has a step-by-step guide to host Python apps through IIS (seems like it could work for Azure App Services as well - although MS suggests using Linux) making use of FastCGI (better approach post 2021 than the CGI approach).
The TL;DR is:
https://docs.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With