ASP脚本中的Python 500服务器错误

8 投票
2 回答
1789 浏览
提问于 2025-04-17 12:54

下面这个ASP脚本在我使用Windows 7(64位)上的IIS 7.5运行时,出现了“HTTP/1.1 500 服务器错误”的提示。

<%@ Language = Python%>
<%
def main():
    Response.Write("My first ASP script!")
main()
%>

在错误日志中,它只提到了一个ASP_0147的错误。

我在服务器上安装了Python 3.2和Active Python 3.2.2.3,并通过pyscript.py注册了Python。

我已经为服务器启用了32位应用程序。我还安装了Windows版的Python,看看这样是否能解决问题。

你能建议我怎么修复这个问题吗?

更新:

我现在已经成功让Python 3工作了,但我必须使用--debug来注册,具体如下:

C:\Python32\Lib\site-packages\win32comext\axscript\client>c:\Python32\python.exe
 pyscript.py --debug
Requesting elevation and retrying...
Registered: Python (for debugging)

为什么它只能在调试模式下工作?在这种模式下运行安全吗?

这是启用调试时的跟踪信息:

Object with win32trace dispatcher created (object=None)
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-SetScriptSite(<PyIActiveScriptSite at 0x00000000036923B0 with obj at 0x000000000056FFD8>,) [1,0,None]
Debugging extensions (axdebug) module does not exist - debugging is disabled.. 
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._QueryInterface_ with unsupported IID IActiveScriptProperty ({4954E0D0-FBC7-11D1-8410-006008C3FBFC})
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-InitNew() [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-GetScriptDispatch(None,) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._QueryInterface_ with unsupported IID {1D044690-8923-11D0-ABD2-00A0C911E8B2} ({1D044690-8923-11D0-ABD2-00A0C911E8B2})
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('Response', 66) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('Request', 66) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('Server', 66) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('Session', 66) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('Application', 66) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('ObjectContext', 66) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('ASPGLOBALTLB', 74) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-ParseScriptText('def main():\r\n    Response.Write("My first ASP script!")\r\nmain()\r\n', None, None, 'STRIP EMBEDDED HTML COMMENTS', 0, 1, 192, 0) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-GetScriptDispatch(None,) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-AddNamedItem('ScriptingNamespace', 10) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-SetScriptState(1,) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-SetScriptState(0,) [1,0,None]
in <win32com.axscript.client.pyscript.PyScript object at 0x00000000035946A0>._InvokeEx_-Close() [1,0,None]

谢谢,

巴里

2 个回答

3

问题出在 win32comext\axscript\client\framework.py 文件中的 trace 方法和 print 语句上。因为在 COM 组件中,像 print 这样的语句写入 sys.stdoutsys.stderr 时会引发异常。例如,在 framework.py 的第 572 行,执行 trace("Debugging extensions (axdebug) module does not exist - debugging is disabled..") 就会导致异常。

一个解决办法是在 framework.py 中添加 import win32traceutil。这样,win32traceutil 会把输出重定向到 win32trace remote collector,解决这个问题,而不需要开启调试,这样可以避免影响性能。

另一个解决办法是把 stdoutstderr 重定向到空值,你可以在 framework.py 的开头添加以下代码片段。

  f = open('nul', 'w')
  sys.stdout = f
  sys.stderr = f

更新:根本原因和解决方案

其实在 framework.py 中已经有机制可以防止 printtrace 语句引发异常,但问题出在 SafeOutput 类的 write 方法上。当没有开启追踪和调试时,write 方法会发生异常,而在异常处理部分调用的 win32api.OutputDebugString 会因为编码不正确而引发异常。因为 win32api.OutputDebugString 需要的是 Unicode 字符串,而不是多字节字符集 (MBCS)。

解决方案:

win32comext\axscript\client\framework.pySafeOutput 类中

class SafeOutput:
softspace=1
def __init__(self, redir=None):
    if redir is None: redir = sys.stdout
    self.redir=redir
def write(self,message):
    try:
        self.redir.write(message)
    except:
        win32api.OutputDebugString(message.encode('mbcs'))
def flush(self):
    pass
def close(self):
    pass

只需要把

win32api.OutputDebugString(message.encode('mbcs'))  # ANSI Enconding

改成

win32api.OutputDebugString(message) # Unicode
5

这可能不是最合适的解决办法,我以前也遇到过这个问题。
最近的ActivePython版本在主动脚本方面似乎有点问题。
我能用的版本是2.5.6.10。
如果版本不太重要的话,你可以试试这个旧版本。

撰写回答