<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=us-ascii" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.6001.18975"></HEAD>
<BODY>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Hi Everybody,</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>In reply to my own mail, because others might run into
the same problem; </SPAN></FONT><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>I received some help from Mark Hammond regarding the
use of Python in WSC's.</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Here's the deal:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>1. It is possible to use a Python WSC from vbscript
ASP, as long as your returnvalues are simple types (strings, integers..)
or COM types (so having a Python WSC that passes an ADO recordset
object from a vbscript WSC to a vbscript ASP page will
work).</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>
<DIV><FONT color=#000000 face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>2. Even
when using Python WSC's and calling them from a Python code block, it is not
possible to pass Python objects to and from the WSC.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>The WSC is
still a "com-layer" between the ASP page and the Python code in the WSC, and it
wants all passed objects to be com variants. You will get an error
like:</SPAN></FONT></DIV>
<DIV><SPAN class=815084612-24112010></SPAN> </DIV>
<DIV><SPAN class=815084612-24112010><FONT color=#000000 face=Courier>Unexpected
Python Error: TypeError: Objects of type 'module' can not be<BR>converted to a
COM VARIANT<BR></FONT></SPAN></DIV>
<DIV><FONT color=#000000 face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT></DIV></SPAN></FONT><FONT color=#0000ff
face=Calibri><SPAN class=815084612-24112010>3. The <FONT color=#000000
face=Courier><implements type="ASP" id="ASP"/></FONT> tag you can normally
use in a WSC doesn't work, so you don't have IIS' objects available. A
workaround for this is to pass these objects from ASP to the WSC
yourself:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>class
IISobjectsClass<BR> public request, response, session,
application, server<BR>end class</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>dim
IISobjects<BR>set IISobjects = new IISobjectsClass<BR> set
IISobjects.request = request<BR> set
IISobjects.response = response<BR> set
IISobjects.session = session<BR> set
IISobjects.application = application<BR> set
IISobjects.server = server</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>'
suppose we have a translation WSC in the current directory, that has open(),
close() and Label() functions defined.<BR>' the open() function could have a
parameter to accept the IISobjects, so you pass them to, and have them
available inside the WSC :<BR>Dim translate<BR>Set translate =
GetObject("script:" & Server.MapPath("./translate.wsc")
)<BR> translate.open(IISobjects)<BR>
Response.Write(translate.label("firstname"))<BR>
translate.close()<BR>Set translate = Nothing</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>4. When an error occurs in the Python code of the WSC,
there will be a very vague ASP error, i.e.: </SPAN></FONT></DIV>
<DIV dir=ltr align=left><SPAN class=815084612-24112010><FONT
face=Calibri></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=815084612-24112010><FONT face=Courier>error
'80020009' <BR>Exception occurred. </FONT></DIV></SPAN>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Mark has concluded that the Python engine -is-
reporting the error, but the script container doesn't pass it to asp. Python
doesn't print the error, assuming asp will show it. A workaround for this is to
decorate the functions in the WSC you want to debug:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Add this function to the top of the Python
WSC:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>def
debug(func):<BR> def callit(*args,
**kw):<BR> try:<BR> return func(*args,
**kw)<BR> except:<BR> import
traceback<BR> traceback.print_exc()<BR> raise<BR> return
callit</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Then add the decorator to the function you want to
debug, any other functions that get called by the function you decorated will
also be debugged:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN
class=815084612-24112010>@debug<BR>def Label(lblName):<BR> """ Returns a
specific label from a Python dictionary """<BR> global d,
new_labels_added<BR> if str(lblName) in d:<BR> return
d[str(lblName)]<BR> else:<BR> d[lblName] = unicode(str(lcid) +
"|" + lblName)<BR> new_labels_added = True<BR> return
str(lcid) + "|" + lblName</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>Any errors
will now show up in a Python trace collector. I have tested this under IIS6 on
XP and IIS7 on Windows 7 x64 (setting the application pool to support 32-bit).
Under IIS6 this works out of the box (when enabling server-side
debugging). Under IIS7 I had to adjust some settings in the IIS
configuration to get it to work. Unfortunately I don't know exactly what
settings I changed on IIS7 to get the errors to be passed from the WSC to ASP
and the trace collector, but at first the "exception occurred" error that IIS6
gave me was not showing up in IIS7, just a blank screen. Messing with some of
the debugging settings in IIS7 made them show up eventually and at that point
the trace collector was also showing me the underlying Python
errors.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>Problem nr.
2 mentioned above (not being able to pass Python objects from a WSC to an ASP
page and vice versa), limits the usability of Python in WSC's severely. There is
another option Mark suggested; using plain Python in ASP (in a <script
runat=server> block) and using Python imports to divide code up into
re-usable parts like this:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=815084612-24112010><script
language="Python" runat="server"><BR>import os<BR>mod_path =
Server.MapPath("./pythonwsc.py")<BR>print "MOD", mod_path<BR>import sys;
sys.path.append(os.path.dirname(mod_path))<BR>from pythonwsc import
list_builtins<BR></script></SPAN></FONT></DIV>
<DIV> </DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>
<DIV><BR><FONT color=#000000 face=Courier><script language="vbscript"
runat="server"><BR> Response.write("<hr/>")<BR> Response.write(list_builtins())<BR> Response.write("<hr/>")<BR></script></FONT></DIV>
<DIV></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>If you
include Python blocks in your page, it is possible to access Python functions
from other languages (i.e. vbscript) if there are multiple of these script
blocks on the page. Please note however that there are some things to consider
regarding the order in which these blocks are processed by IIS: <A
href="http://phrogz.net/tmp/serversidejsandvb.html"><FONT
face="Times New Roman">http://phrogz.net/tmp/serversidejsandvb.html</FONT></A></SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>Using this
method Python can access all of IIS' special objects as
well.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>I'm still
struggling now to get the Python imports to work like my WSC's work, so I don't
have to change the syntax throughout my pages; i.e.; I have a "translate" wsc,
and currently I can translate parts of a webpage by saying for
example:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Translate.Label("pagetitle")</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>In the
Python example above I would import "translate.py" and then call the Label()
function directly, so without the leading "Translate.". In order to maintain the
same syntax I guess I need to write a class inside the .py file and import that,
but being new to Python I haven't yet figured out how that works
exactly.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>Hopefully
this helps some other people struggling with Python/classic asp/WSC's, if there
still are any :)</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN class=815084612-24112010>Thanks to
Mark for all of his help.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN
class=815084612-24112010>Erik</SPAN></FONT></DIV></BODY></HTML>