Solved

Python on IIS

Posted on 2015-01-14
3
372 Views
Last Modified: 2015-01-14
Hello Expert,

Working on getting Python to run from IIS
Following instructions from
http://stackoverflow.com/questions/6823316/python-on-iis-how

Basically the idea is
1. Unpack and install that python MSI. Choose the default, which puts python into c:\Python27
2. Create a directory to hold the "development" python scripts. Eg, c:\dev\python
3. Set the permissions on the files in the directory c:\dev\python to allow IIS to read and execute
4. Create a new IIS application. Specify the virtual path as /py and the physical path as c:\dev\python
5. Within the IIS application, add a script map for *.py, and map it to c:\python27\python.exe %s %s
6. Create a "HelloWorld.py" file in c:\dev\python with this as the content:

print 'Content-Type: text/plain'
print ''
print 'Hello, world!'

7. Invoke http://localhost/py/helloworld.py

When the helloworld.py is invoked the result is

HTTP Error 502.2 - Bad Gateway
The specified CGI application misbehaved by not returning a complete set of HTTP headers.
The headers it did return are "C:\Python27\python.exe: can't find '__main__' module in 'c:\\dev\\python' ".

Some articles on Python and 'CGI application misbehaved ' were read. One said to add
print "HTTP/1.0 200 OK\n";
print "Content-Type: text/html\n\n\n";

another said add
#!c:/python26/python.exe
# original from http://python.about.com/od/cgiformswithpython/ss/test_cgi_all.htm
#
import sys
sys.stderr = sys.stdout
import os
from cgi import escape

Neither of these worked.

Any idea how to troubleshoot CGI or fix the problem?

Thanks

Allen in Dallas
0
Comment
Question by:9apit
  • 2
3 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40549719
I have Python in a directory outside the web root.  So I added it as a 'Virtual Directory' and copied your HelloWorld.py and it ran just fine.  I already had Python set up.  I am running IIS 5.1 (XP) and Python 2.7.  IIS 5.1 is simpler than the ones that follow.

So the basic concept works but there is something blocking it in your setup.  Have you added Python to your Path statement?
0
 

Author Comment

by:9apit
ID: 40549771
Hello Baldwin,

I found the problem.
With the py application selected in IIS the Handler Mappings was clicked.
Right clicked on mapping called Python and chose Edit.
Noticed that the set instructions said that in 'Request Map'
the path should be *.py.

I left out the dot between the asterisk and the p.

So I got it to work.

I have used ASP, ASP.Net and JavaScript. In ASP the server
side script in an HTML page is included in tags  like <% server script /%>
and in Javascript it is <SCRIPT>client script</SCRIPT>

How do you include Python in an HTML page?

If you want I can open another questions for this.

Thanks,

Allen in Dallas
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40549797
How do you include Python in an HTML page?
I've never seen it done that way.  Python, like Perl, is usually used to create a web page thru 'print' statements.  I don't know of a way to 'embed' it in a web / HTML page.

In my IIS, the mapping is '.py', with the dot and without the star '*'.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now