在ASP中,为什么我可以从VBScript调用Python函数,但反之不行?

2 投票
2 回答
1454 浏览
提问于 2025-04-15 18:36

我打算为一个老旧的ASP应用写一些新的代码,使用的是Python,但遇到了一些奇怪的问题。如果我在Python里写一个函数,我可以很容易地从VBScript代码块中调用它。然而,如果我试图从Python调用一个在VBScript中定义的函数,就会出现错误:

Python ActiveX Scripting Engine error '80020009'

Traceback (most recent call last): File "<Script Block >", line 3, in <module> PrintVBS() NameError: name 'PrintVBS' is not defined

/test.asp, line 20

这里有一个简单的例子来演示这个问题:

<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>

有没有人能解释一下这种情况?有没有什么解决办法?

需要说明的是,我知道我可以把我的VBScript代码放在一个WSC文件里,但我觉得那样很麻烦,如果能避免的话,我希望不去做。

2 个回答

1

我也在做同样的事情。我发现通过在Python中注册一个回调函数(也就是明确告诉Python这个函数的存在),我似乎成功了。关键是,VBScript必须先调用Python,这样Python才能再调用回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()
%>
3

这可能跟