<!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>&nbsp;</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&nbsp;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>&nbsp;</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&nbsp;COM types (so having a Python WSC that passes an ADO&nbsp;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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&lt;implements type="ASP" id="ASP"/&gt;</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&nbsp;to the WSC 
yourself:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN 
class=815084612-24112010></SPAN></FONT>&nbsp;</DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>class 
IISobjectsClass<BR>&nbsp;&nbsp;&nbsp; public request, response, session, 
application, server<BR>end class</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT>&nbsp;</DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>dim 
IISobjects<BR>set IISobjects = new IISobjectsClass<BR>&nbsp;&nbsp;&nbsp; set 
IISobjects.request&nbsp; &nbsp;= request<BR>&nbsp;&nbsp;&nbsp; set 
IISobjects.response&nbsp; &nbsp;= response<BR>&nbsp;&nbsp;&nbsp; set 
IISobjects.session&nbsp; &nbsp;= session<BR>&nbsp;&nbsp;&nbsp; set 
IISobjects.application &nbsp;= application<BR>&nbsp;&nbsp;&nbsp; set 
IISobjects.server&nbsp; &nbsp;&nbsp;= server</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT>&nbsp;</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,&nbsp;so you pass them to, and have them 
available inside the WSC&nbsp;:<BR>Dim translate<BR>Set translate = 
GetObject("script:" &amp; Server.MapPath("./translate.wsc") 
)<BR>&nbsp;&nbsp;&nbsp; translate.open(IISobjects)<BR>&nbsp;&nbsp;&nbsp; 
Response.Write(translate.label("firstname"))<BR>&nbsp;&nbsp;&nbsp; 
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>&nbsp;</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.:&nbsp;</SPAN></FONT></DIV>
<DIV dir=ltr align=left><SPAN class=815084612-24112010><FONT 
face=Calibri></FONT></SPAN>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN class=815084612-24112010>def 
debug(func):<BR>&nbsp;def callit(*args, 
**kw):<BR>&nbsp;&nbsp;try:<BR>&nbsp;&nbsp;&nbsp;return func(*args, 
**kw)<BR>&nbsp;&nbsp;except:<BR>&nbsp;&nbsp;&nbsp;import 
traceback<BR>&nbsp;&nbsp;&nbsp;traceback.print_exc()<BR>&nbsp;&nbsp;&nbsp;raise<BR>&nbsp;return 
callit</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff face=Calibri><SPAN 
class=815084612-24112010></SPAN></FONT>&nbsp;</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>&nbsp;</DIV>
<DIV dir=ltr align=left><FONT face=Courier><SPAN 
class=815084612-24112010>@debug<BR>def Label(lblName):<BR>&nbsp;""" Returns a 
specific label from a Python dictionary """<BR>&nbsp;global d, 
new_labels_added<BR>&nbsp;if str(lblName) in d:<BR>&nbsp;&nbsp;return 
d[str(lblName)]<BR>&nbsp;else:<BR>&nbsp;&nbsp;d[lblName] = unicode(str(lcid) + 
"|" + lblName)<BR>&nbsp;&nbsp;new_labels_added = True<BR>&nbsp;&nbsp;return 
str(lcid) + "|" + lblName</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri></FONT>&nbsp;</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&nbsp;works out of the box (when enabling server-side 
debugging).&nbsp;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>&nbsp;</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 &lt;script 
runat=server&gt; 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>&nbsp;</DIV>
<DIV><FONT face=Courier><SPAN class=815084612-24112010>&lt;script 
language="Python" runat="server"&gt;<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>&lt;/script&gt;</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV><FONT color=#0000ff face=Calibri><SPAN 
class=815084612-24112010>
<DIV><BR><FONT color=#000000 face=Courier>&lt;script language="vbscript" 
runat="server"&gt;<BR>&nbsp;Response.write("&lt;hr/&gt;")<BR>&nbsp;Response.write(list_builtins())<BR>&nbsp;Response.write("&lt;hr/&gt;")<BR>&lt;/script&gt;</FONT></DIV>
<DIV></SPAN></FONT>&nbsp;</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&nbsp;objects as 
well.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN 
class=815084612-24112010></SPAN></FONT>&nbsp;</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&nbsp;of a webpage by saying for 
example:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN 
class=815084612-24112010></SPAN></FONT>&nbsp;</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>&nbsp;</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>&nbsp;</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&nbsp;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>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Calibri><SPAN 
class=815084612-24112010>Erik</SPAN></FONT></DIV></BODY></HTML>