【问题标题】:500 Server Error for Python in ASP ScriptASP 脚本中 Python 的 500 服务器错误
【发布时间】:2012-03-04 09:07:21
【问题描述】:

下面的 asp 脚本给了我错误:“HTTP/1.1 500 Server Error”

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

当我在 IIS 7.5 Windows 7(64 位)上运行它时。在错误日志中它只是提到了一个 ASP_0147 错误。

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

我已为服务器启用 32 位应用程序。我还为 Windows 安装了 Python,看看是否有帮助。

你能建议我如何解决这个问题吗?

更新:

我现在已经设法让它在 python3 上工作,但我必须注册 --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]

谢谢,

巴里

【问题讨论】:

  • 启用 32 位应用程序是应用程序池上的设置,但仅当您的服务器是 64 位时才相关。类似的一段 vbscript 代码运行正常吗?
  • 您的 ASP 有 Response.Write("My first ASP script!"),但您的调试跟踪显示 Response.Write("My third ASP script!")\r\n。你能检查它是同一个页面还是不同的页面。
  • @user568109 抱歉,两者的消息相同。我已经在上面解决了这个问题!
  • @Baz 我测试了我的解决方案,它可以在 IIS 7.5 Windows 7(64 位)上与 Python 3.2 和 Active Python 3.2.2.3 一起正常工作
  • @Baz 我想知道你有没有机会尝试解决方案?

标签: python asp-classic iis-7.5 activepython


【解决方案1】:

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

一种解决方法是在framework.py 中添加import win32traceutilwin32traceutil 将输出重定向到win32trace remote collector 并解决问题而无需启用调试女巫导致性能问题。

另一种解决方法是将stdoutstderr重定向为null,您可以在framework.py顶部添加以下代码sn-p。

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

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

framework.py 中已经有一种机制可以防止打印和跟踪语句引发异常,但问题出在SafeOutput 类的write 方法中。未启用跟踪和调试时,在 write 方法中会发生异常,并且 except 子句中的win32api.OutputDebugString 将使用错误的编码调用导致异常。因为win32api.OutputDebugString 接受 Unicode 字符串而不是多字节字符集 (MBCS) 作为参数。

解决办法:

win32comext\axscript\client\framework.py 中在SafeOutput 类中

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

【讨论】:

    【解决方案2】:

    可能不是合适的解决方案,过去我遇到过这个问题。
    最新版本的 activepython 似乎不适合活动脚本。
    我只能使用 2.5.6.10 版本。
    如果版本不重要,您可以尝试使用旧版本。

    【讨论】:

    • 我已经阅读了一些关于此的内容,但我仍然设法让它在我的 windowsXP 机器上使用 Python 3.2 工作。
    • 虽然我必须承认,如果我在 Windows 机器上的 IIS 管理器中浏览,如果我尝试加载我的页面,我会收到 500 错误。然后我需要为此网站重新启动 iis 以使页面再次工作。
    • 我明白了。我所有的尝试都是在 Windows 7 上。我已经放弃了 :) 您可以尝试注册 pyscript.py --debug 并使用 Pythonwin.exe 进行跟踪。也许你能找到一些东西。
    • 现在在服务器上安装了 Active Python 2.5.6.10,它按预期工作。有谁知道使它适用于 3.2 的修复程序?谢谢!
    • 我在注册 2.7 版本之前安装了 2.7 并在 2.5 版本上运行了“python pyscript.py --uninstall”。卸载表明 2.5 已卸载。从我的 asp 页面打印 sys.version_info 给我 (2, 5, 6, 'final', 0)。那么 pyscript.py 是做什么的呢?我很困惑...
    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2010-11-30
    • 2013-02-09
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多