8

The following asp script is giving me the error: "HTTP/1.1 500 Server Error"

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

when I run it on IIS 7.5 Windows 7 (64 bit). In the error log it simply mentions an ASP_0147 error.

I have installed Python 3.2 and Active Python 3.2.2.3 on the server and registered Python via: pyscript.py

I have enabled 32-bit applications for the server. I have also installed Python for Windows to see if that would help.

Can you suggest how I might fix this?

UPDATE:

I have managed to get this working now for python3 but I have to register with --debug, as follows:

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

Why will it only work in debug mode? Is it safe to run in this mode?

Here's the trace when debug is enabled:

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]

Thanks,

Barry

Karan Alangat
  • 2,154
  • 4
  • 25
  • 56
Baz
  • 12,713
  • 38
  • 145
  • 268
  • The enable 32 bit applications is a setting on the application pool, but only relevant if your server is 64 bit. Does a similar piece of vbscript code run correctly? – Erik Oosterwaal Feb 13 '12 at 09:01
  • Your ASP has `Response.Write("My first ASP script!")`, but your debug trace shows `Response.Write("My third ASP script!")\r\n`. Can you check if it is the same page or different page. – user568109 May 31 '13 at 04:06
  • @user568109 Sorry both have the same message. I've fixed this above! – Baz May 31 '13 at 12:40
  • @Baz I test my solution and it work fine with Python 3.2 and Active Python 3.2.2.3 on IIS 7.5 Windows 7 (64 bit) – Kambiz Shahim Jun 04 '13 at 14:03
  • @Baz I was wondering have you any chance to try the solution? – Kambiz Shahim Jun 09 '13 at 08:48
  • @Kambiz Shahim Thanks very much for your answer! I will try it soon when I have get a chance! – Baz Jun 10 '13 at 09:41

2 Answers2

5

May not be the appropriate solution, in the past I've had this problem.
The recent versions of activepython seems broken for active scripting.
I was able just the version 2.5.6.10.
If the version is not important, you could try that older version.

Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • I have read something about this but I still managed to get it to work on my windowsXP machine with Python 3.2. – Baz Feb 13 '12 at 19:56
  • Although I must admit that if I browse within IIS Manager on my windows machine, I get the 500 Error if I try to load my page. I then need to restart iis for this website in order for the page to work again. – Baz Feb 13 '12 at 20:03
  • I understand. All my attempts was on Windows 7. I had give up :) You can try to register with `pyscript.py --debug` and trace using Pythonwin.exe. Maybe you can find something. – Kul-Tigin Feb 13 '12 at 20:10
  • Installed Active Python 2.5.6.10 on the server at now it works as expected. Does anyone know of a fix to get it to work for 3.2? Thanks! – Baz Feb 13 '12 at 20:31
  • I installed 2.7 and ran "python pyscript.py --uninstall" on the 2.5 version before registering the 2.7 version instead. Uninstalling states that 2.5 is uninstalled. Printing sys.version_info from my asp page gives me (2, 5, 6, 'final', 0). So what does pyscript.py do? I'm confused... – Baz Feb 13 '12 at 22:10
  • You could try installing the latest version of pywin32 manually maybe – Erik Oosterwaal Feb 14 '12 at 08:17
3

The problem is trace method and print statements in the win32comext\axscript\client\framework.py because in the COM components writing to the sys.stdout or sys.stderr like the print statement causes an exception for example trace("Debugging extensions (axdebug) module does not exist - debugging is disabled..") at the line 572 of framework.py causes an exception.

One workaround is adding import win32traceutil in the framework.py. The win32traceutil redirects output to win32trace remote collector and solve the problem without need to enabling debugging witch cause performance issues.

Another workaround is redirecting stdout and stderr to null, you can add following code snippet at the top of framework.py.

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

UPDATE: The root cause and the solution

There is already a mechanism in framework.py to prevent print and trace statements to raise an exception but the problem is in the write method of SafeOutput class. When tracing and debugging is not enabled, in the write method an exception occurs and win32api.OutputDebugString in the except clause will invoke with the wrong encoding which cause an exception. Because the win32api.OutputDebugString accept Unicode string not a multibyte character set (MBCS) as an argument.

The solution:

in the win32comext\axscript\client\framework.py in the SafeOutput class

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

just change

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

to

win32api.OutputDebugString(message) # Unicode
Kambiz Shahim
  • 2,560
  • 14
  • 21