Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help! IIS and Python

3 views
Skip to first unread message

Sunit Joshi

unread,
Feb 13, 2002, 3:45:32 PM2/13/02
to
Hello
I'm trying to use IIS5.0 with Python 2.1 and whenever I try to import
modules I get an error. The test.asp and error are below.
thanks
Sunit
sjo...@ingr.com

<%@ Language=Python %>
<%
import string
server = Request.ServerVariables("SERVER_NAME")
server = string.upper(server)
clientIP = Request.ServerVariables("REMOTE_ADDR")
%>
<HTML><HEAD><TITLE>Python Test Page</TITLE></HEAD>
<BODY>
<p>
<h3><font color="#990033">You are coming from <b><font
color="#669900"><%=clientIP%></font></b>
to <%=server%></font></h3>

<hr>
PythonAspTest
</BODY>
</HTML>
Error Type:
Python ActiveX Scripting Engine (0x80020009)
Traceback (innermost last): File "<Script Block >", line 3, in ?
server = string.upper(server) File "C:\Python21\lib\string.py", line
60, in upper return s.upper() File
"C:\Python21\win32com\client\dynamic.py", line 438, in __getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: <unknown>.upper

Steve Holden

unread,
Feb 13, 2002, 4:00:50 PM2/13/02
to
"Sunit Joshi" <sjo...@ingr.com> wrote in message
news:8f8ffe67.02021...@posting.google.com...

The problem here appears to be that you are probably getting a COM object of
some sort back as a result of your call to Request.ServerVariables(), and
when you try to apply the upper() function from the string module it
complains that you aren't apssing the right kind of object as an argument.

If you replace

server = Request.ServerVariables("SERVER_NAME")

with

server = str(Request.ServerVariables("SERVER_NAME"))

for example, you will get a Python string back and all should be copacetic.

I personally I find it so tiresome to interact with the Scripting
environment in this way I have now moved away form IIS completely, and use
Apache or Xitami as a web server. I recently had to go back to coding
VBScript for IIS, and it was painful.

regards
Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python. We don't care much about theory, except where it
intersects with useful practice." Aahz Maruch on c.l.py


Tim Roberts

unread,
Feb 14, 2002, 1:23:41 AM2/14/02
to
sjo...@ingr.com (Sunit Joshi) wrote:
>
>I'm trying to use IIS5.0 with Python 2.1 and whenever I try to import
>modules I get an error. The test.asp and error are below.
>thanks

It wouldn't have solved your problem, but it might help you to know that
you do not need to import the string module any more. All of the functions
in the string module are now methods of string objects. That is, instead
of:
x = string.upper( "xyz" )
you can say:
x = "xyz".upper()

And, in fact, the code in the string module now does just that:

def upper(s)
return s.upper()

Since we're on the subject of Python in ASP programming, how do you use the
<%=xxx%> trick with indentation? This fails with an indentation error, for
example:

<%
for i in range(5):
%>
<%="aaa%d" % i %>

--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Max M

unread,
Feb 14, 2002, 4:14:54 AM2/14/02
to
Tim Roberts wrote:

> Since we're on the subject of Python in ASP programming, how do you
use the
> <%=xxx%> trick with indentation? This fails with an indentation
error, for
> example:
>
> <%
> for i in range(5):
> %>
> <%="aaa%d" % i %>

Generally I would say "don't do it". every codeblock is seen as a new
block, so your code looks to Python like:

for i in range(5):
Response.Write("aaa%d" % i)

Perhaps this would work, I don't know:

<%
for i in range(5):
%>
<%= "aaa%d" % i %>

You need to put all your logic before the html code, and then insert
simple strings. This approach is also a faster for lots of content as
joining a long list is a LOT faster than concatenating many strings.

Like:

<%
a3Nums = []
for i in range(5):
a3Nums.append("aaa%d" % i)
a3Nums = '\n'.join(a3Nums)
%>

<%= a3Nums%>

It also makes your code much more readable. Mixing logic and
presentation is really only a good idea for very simple pages.

regards Max M

gbr...@cix.compulink.co.uk

unread,
Feb 14, 2002, 6:32:11 AM2/14/02
to
Tim Roberts wrote:

> Since we're on the subject of Python in ASP programming, how do you use
> the
> <%=xxx%> trick with indentation? This fails with an indentation error,
> > for
> example:
>
> <%
> for i in range(5):
> %>
> <%="aaa%d" % i %>

<%
formatter = """
aaa%d
"""

for i in range(5):
Response.Write(formatter%i)
%>

Why not?


Graham

Tim Roberts

unread,
Feb 16, 2002, 11:43:09 PM2/16/02
to

We're diving deeply into religion and philosophy here. Partly, it's just
the principle of the thing; I assumed there was a magic method to do this
with Python, but I'm coming to the conclusion that there is not. The
"Response.Write" call seems extraneous to me; VBScript and JScript forms
can omit it with the <%= trick, as if we could write this:

...


for i in range(5):
%>

<%= formatter % i %>

Perhaps the issue is that I don't know how the <%= form is actually
implemented in Python/ASP. If it is just a textual substitition, where the
<%= ... %> is replaced with Response.Write(...) by simple textual
substitution, then I understand why it is not possible.

Jason Orendorff

unread,
Feb 16, 2002, 11:42:28 PM2/16/02
to
> ...
> for i in range(5):
> %>
> <%= formatter % i %>
>
> Perhaps the issue is that I don't know how the <%= form is actually
> implemented in Python/ASP. If it is just a textual substitition
> [...], then I understand why it is not possible.

It is a textual substitution.

I'd be tempted to try something like:

...
for i in range(5):
%>

<p>Foo...


<%= formatter % i %>

...bar</p>
<%
%>

Note that I have the '%>'s and '<%'s rather carefully
lined up as if they were Python statements.

## Jason Orendorff http://www.jorendorff.com/


Steve Holden

unread,
Feb 20, 2002, 7:53:45 AM2/20/02
to
"Tim Roberts" <ti...@probo.com> wrote ...

<%= ... %> is indeed a simple macro for Response.Write(...).

The real problem is that ASP has no knowledge of indentation-sensitivity in
a language, and tends (as far as I can see) to pass blocks of code
individually to the interpreter.

It makes a lot more sense in Python to generate the whole HTML string before
emitting it with a single Response.Write() call. This anyway suits the
philosophy of modules such as HTMLGen rather better, and allows much simpler
exception handling than if you've already emitted half of your HTML!

Jim Abrams

unread,
Feb 26, 2002, 4:12:21 PM2/26/02
to
"Steve Holden" <sho...@holdenweb.com> wrote in
news:ZtAa8.211294$KM2.8...@atlpnn01.usenetserver.com:

> The problem here appears to be that you are probably getting a COM
> object of some sort back as a result of your call to
> Request.ServerVariables(), and when you try to apply the upper()
> function from the string module it complains that you aren't apssing
> the right kind of object as an argument.
>
> If you replace
>
> server = Request.ServerVariables("SERVER_NAME")
>
> with
>
> server = str(Request.ServerVariables("SERVER_NAME"))
>
> for example, you will get a Python string back and all should be
> copacetic.
>
> I personally I find it so tiresome to interact with the Scripting
> environment in this way I have now moved away form IIS completely, and
> use Apache or Xitami as a web server. I recently had to go back to
> coding VBScript for IIS, and it was painful.

If, like me, you don't have the option of moving away from IIS, you can
save yourself alot of headache, time, and resources by writing a nice
wrapper object, sticking Response inside and using your own. You can then
add sorely lacking functionality, like str conversion, to it as well as any
additional shortcuts.

I do it for most of the built ins (Response, Request, Server, Appication,
Session) and it's been a lifesaver.

Also (in reply to somewhere in this thread) you can do a quick:
from win32com.axscript.client.framework import SafeOutput
_out = SafeOutput(Response)

then use
print >> _out, Stuff

to get the handy print str conversion. Which Response.Write doesn't do.
Don't use sys.stdout = etc and regular print.

In addition to HTMLgen, which is almost a requirement for Python in ASP,
check out the string iterpolation module, it also has come it quite handy
for me. Itpl.py

-Jim

Max M

unread,
Feb 26, 2002, 5:22:46 PM2/26/02
to
Jim Abrams wrote:


> If, like me, you don't have the option of moving away from IIS, you can
> save yourself alot of headache, time, and resources by writing a nice
> wrapper object, sticking Response inside and using your own. You can then
> add sorely lacking functionality, like str conversion, to it as well as any
> additional shortcuts.
>
> I do it for most of the built ins (Response, Request, Server, Appication,
> Session) and it's been a lifesaver.

If thats your bag, feel free to use my 'iisUtils' module.

It both has a windows version of Pythons FieldStorage, and a Dict
version of the Request.

To get FieldStorage:

import iisUtils

fs = iisUtils.winFieldStorage(Request)

<%= fs['somevar'].value%>

This is good because it can handle file uploads. But it's a bother that
it's not a stright dict for the rest.

So to get Request as a plain dict:

req = iisUtils.request(Request)

<%= req['somevar']%>

etc.

Its documented in the code and works nicely in production code. If there
are _many_ fields in the form 'request' is faster than 'winFieldStorage'.

regards Max M

iisUtils.py
0 new messages