开发者

In ASP - Why is it that I can call python functions from vbscript but not vice-versa?

I'm planning on writting some new code for a legacy ASP application in Python, and I ran into some odd behavior. If I write a function in python I can easily call it from a VBScript block. However, if I try to call a function defined in VBScript from python, I get an error:

Python ActiveX Scripting Engine error '80020009'

Traceback (most recent call last): File "<Script Bl开发者_Python百科ock >", line 3, in <module> PrintVBS() NameError: name 'PrintVBS' is not defined

/test.asp, line 20

Here is a quick example demonstrating the problem:

<script language="Python" runat="server">
def PrintPython():
    Response.Write( "I'm from python<br>" )
</script>

<script language="vbscript" runat="server">
Sub PrintVBS()
    Response.Write( "I'm from VBScript<br>" )
End Sub
</script>

<script language="vbscript" runat="server">
PrintVBS()
PrintPython()
</script>


<script language="python" runat="server">
PrintPython() # code is fine up to here, 
PrintVBS() # no error if you comment this line
</script>

Does anyone have any insight into this behavior? Any workarounds?

Note, I am aware that I could throw my vbscript code in a WSC file, but I find them a royal pain to work with, and I'd like to avoid that if at all possible.


It probably has to do with the order in which the script tags are processed.

In this case, it seems that the script tags containing python code are processed first, and then the ones with vbscript. The result is that you are trying to call PrintVBS() before it is available.

If you would change the default language to python, you would probably get the reverse error.


I'm doing the same thing. I seem to be having success by registering a callback with Python (i.e. explicitly telling Python about the function). The trick is that VBScript must call into Python for Python to be able to call back out into VBScript.

<%@LANGUAGE="VBSCRIPT"%>
<script language="Python" runat="server">
_PrintVBS = None
def register_printvbs(callback):
    global _PrintVBS
    _PrintVBS = callback

def PrintPython():
    Response.Write( "I'm from python<br>" )
</script>

<%
Sub PrintVBS()
    Response.Write( "I'm from VBScript<br>" )
End Sub
Call register_printvbs(GetRef("PrintVBS"))
PrintVBS()
PrintPython()
%>

<script language="python" runat="server">
def python_test():
    PrintPython() # code is fine up to here, 
    _PrintVBS() # no error if you comment this line
</script>

<%
Call python_test()
%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜