67

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?

the
  • 21,007
  • 11
  • 68
  • 101
Robert
  • 671
  • 1
  • 6
  • 3
  • 2
    Have you spotted ISAPI-WSGI? http://code.google.com/p/isapi-wsgi/ (WSGI is the modern way to run Python inside a webserver) – Thomas K Jul 25 '11 at 22:57
  • 1
    PyISAPIe also supports WSGI. It has horrible documentation, but is actually simpler to setup than ISAPI-WSGI. – sayap Jul 26 '11 at 01:10

5 Answers5

145

I just did this in 5 minutes.

  1. Ensure 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.

    enter image description here

  2. 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.

  3. Unpack and install that python MSI. Choose the default, which puts python into c:\Python27

  4. Create a directory to hold your "development" python scripts. Eg, c:\dev\python

  5. 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)"
    
  6. 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.

    enter image description here

    enter image description here

  7. Within that IIS application, add a script map for *.py, and map it to c:\python27\python.exe %s %s

    enter image description here

    enter image description here

    enter image description here

  8. create a "HelloWorld.py" file in c:\dev\python with this as the content:

     print('Content-Type: text/plain')
     print('')
     print('Hello, world!')
    
  9. invoke http://localhost/py/helloworld.py

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • I keep getting an error The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are " File "C:\PythonApp\helloworld.py", line 1 print "Content-Type: text/plain;charset=utf-8" ^ SyntaxError: Missing parentheses in call to 'print' ". after going through these steps. Note: I am using Python 34 instead of 27 in the example – KnightFox Jun 21 '15 at 22:15
  • @KnightFox The error you are seeing is because of the difference between Python 3.x and Python 2.x. Python 3.x requires parantheses for print as it's a function call, unlike Python 2.x in which it's a statement. – Dr. Nitin Reddy Katkam Jul 04 '15 at 04:59
  • I was following these steps and at the end I got the error: 'the specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "".'.CGI is installed. In my project I got also got file of extension ".wsgi". I just got this project and need to deploy it on IIS. Maybe this file is related to the error that I got (not sure). Do you have any idea @Dr.NitinReddyKatkam @Cheeso? – delux Feb 11 '16 at 00:06
  • 11
    This is literally the worst way to deploy python apps on IIS. Running as pure CGI means that server has to create new process on each request which is dog slow (as in single or two digits rps top). Using ISAPI/FastCGI doesn't take setup any longer and works way better. – Zart Oct 19 '16 at 05:32
  • I download this one Windows x86-64 MSI installer and it says is for amd64 does this only works on those processors or this one is fine if i have a x64 windows operating system? – Raul H Nov 07 '16 at 19:53
  • 'amd64' is the name of the instruction set, nothing to do with amd processors. – CodeCabbie Feb 15 '17 at 09:16
  • 5
    @Zart Okay, if it's faster and just as easy to set it up with FastCGI, what are the steps to accomplish this (or which steps are different?) – Typel Aug 24 '17 at 18:08
  • 4
    what's the details behind the `%s %s` after the python executable? – solstice333 Oct 13 '17 at 17:05
  • 1
    This just 404's in 2018 – JacobIRR May 18 '18 at 16:36
  • Is that regular CGI or FastCGI? – jpmc26 Jul 11 '18 at 22:26
  • 1
    Usin powershell for Windows Features, install python, create website, etc ? Flask ? – PreguntonCojoneroCabrón Feb 02 '19 at 10:28
  • I have 64-bit windows but it is not taking 64-bit python but run successfully on 32-bit python. Why??. I'm using Python 3.5 – Parikshit Chalke Mar 29 '19 at 07:24
  • 1
    @JacobIRR, I got it running the same way on Windows 2016, took me only 3 days ;-) But the point why it took me so long was that I forgot to give IUSR and IIS_IUSR modify rights on the pythonxx-32 folder. First I got a 404 like you, but than I added %s %s and I got a 502, than I added the modify rights and it worked. – Martin Lietz May 24 '19 at 15:09
  • One note, if the path has spaces put quotes around the path and executable, but not the `%s %s`, so like this: `"C:\Program Files\Python36\python.exe" %s %s` – sniperd Aug 01 '20 at 13:25
  • IIS will tell us to use double quotes when needed as @sniperd indicates but for me I had to also use double quotes for the first `%s`. No one answered the question about `%s %s` so I do not know about the second `%s`. – Sam Hobbs Oct 01 '21 at 00:40
  • I got this working in a few minutes, thanks! is it possible to read the query string of the page? perhaps via the command line arguments? – Phillip Trelford Feb 09 '22 at 13:06
  • The folder of \Python\ should also have access permission for IUSR and IIS_IUSR. – Ehsan Abidi Aug 14 '23 at 12:15
4

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

sniperd
  • 5,124
  • 6
  • 28
  • 44
YEH
  • 374
  • 4
  • 18
  • 1
    Are you sure that your problem was not just access rights? – Andreas Venieris Mar 27 '17 at 10:12
  • 1
    Yes, it was a special characters problem in the directory or maybe total length of path, but not access rights because I did try all obvious solutions – YEH Mar 28 '17 at 14:04
  • @AndreasVenieris Same thing happened to me as well. I have resolved it by moving it from "Program Files" to directly C: drive. My last path was "C:\Python" and it worked. Even the double quote method did not work for me. Go figure. – platypus Oct 20 '21 at 14:48
4

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:

  • Install Python for Windows
  • Install wfastcgi package per https://pypi.org/project/wfastcgi/
  • Configure your web.config file to point to whichever framework your Python service is wrapped in (eg. Bottle, Flask, Django)

https://learn.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022

Johan Foley
  • 338
  • 4
  • 9
-1

Hey guys you dont nedd to use IIS when you want to migrate your apps to python, you only have to do is to use a library named flask coming whitin standard libraries or alternative use bottle or http components to do that , and you can still use iis too, py apps can works on other port like 3000 while iis continue with port 80.

Bye

Henry
  • 1
-3

When you are developing a web application with Python, you don't use IIS/Apache/etc. Those web servers are only for deployment. Frameworks like Pyramid/Pylons/Django all come with built-in web servers. Pyramid, in particular, has excellent documentation which should help you to get started: http://docs.pylonsproject.org/docs/pyramid.html

When you get to the point of deployment, Linux + Apache would be a much saner choice than Windows + IIS. If you absolutely must use Windows + IIS, don't use isapi-wsgi, as it has phantom performance problem: http://groups.google.com/group/isapi_wsgi-dev/browse_thread/thread/9fade6efca6c5b89

PyISAPIe has worked well enough for me, but I had to compile my own PyISAPIe.dll for Python 2.7.

sayap
  • 6,169
  • 2
  • 36
  • 40
  • 1
    Thanks. I think compiling PyISAPe.dll for Python 2.7 is my next step, (maybe it should be hosted somewhere for download). My application is complex enough to have the need to intervene on the live server since reproducing a problem is close to impossible (500 gb of data, users running program-paths that are almost determined individually). I've seen plenty of cases where people develop on their safe laptops, deploy, and then stuff does unexpected things. – Robert Jul 30 '11 at 06:48
  • 14
    Question wasn't what alternatives to use, was to use IIS – Mastro Apr 15 '14 at 16:04