ActiveState Community

How to debug WSGI server based application

Posted by rcscomp on 2008-09-30 16:21

I have a framework built on top of Werkzeug, which is built on the wsgiref wsgi server. How would I go about debugging this in komodo? I have debugging of regular python scripts figured out, but whenever I use the framework my breakpoints never get reached.

Thanks.

toddw | Tue, 2008-09-30 16:28

You'll need to use Komodo's remote debugging features in order to properly debug a WSGI application. There is a good description of the required steps in the Django debugging article:
http://community.activestate.com/forum-topic/debugging-django-apps#comme...

Let us know if you have problems getting this working.

Cheers,
Todd

rcscomp | Wed, 2008-10-01 07:58

Todd,

Thanks for pointing that out. I was able to figure out what I needed from that post and from the TurboGears one:

http://community.activestate.com/forum-topic/debugging-turbogears

Basically, all I head to do was add this at the top of my main "runserver" script (AFTER I made sure dbgp was on my PYTHONPATH):

import dbgp.client
dbgp.client.brk(host="localhost", port=1800)

making sure the port was set to what Komodo showed in Debug->Listener Status

Caveats:

1) Multiple debug sessions get started if I am using a reloader. I decided to just skip reloading when debugging (this was noted in the documentation)
2) I get a *bunch* of erroneous output when doing this that says "SystemError('error return without exception..."

Any thoughts on how to get rid of that error output?

toddw | Wed, 2008-10-01 10:33

It is good that you've got the debugging figured out. For the error "SystemError('error return without exception...", could you log a bug for that at:
http://bugs.activestate.com/enter_bug.cgi?product=komodo

Please include how to reproduce this error and the full traceback exceptions, which you can find in Komodo's error log:
http://community.activestate.com/faq/komodo-file-locations#log_files

Thanks,
Todd

shanec | Sun, 2008-10-05 13:01

Here's a simple example of a wsgi application (in this case Trac) debuggable with dbgp. You'll need PyDBGP installed. Apache will need to be configured to use this script as the wsgi process script (e.g. WSGIScriptAlias points to this). See the google url in the code for more information. I have only tested this with Apache and mod_wsgi.

#!/usr/bin/python
# -*- coding: utf-8 -*-
# This is a simple mod_wsgi script for Trac
# See also:
#   http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac
#   http://trac.edgewall.org/wiki/TracModWSGI
#
#   http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
#   this should be run via httpd -x

import sys
sys.stdout = sys.stderr

from trac.web.main import dispatch_request

class Debugger:

    def __init__(self, object):
        self.__object = object

    def __call__(self, *args, **kwargs):
        environ = args[0]
        # set dbgp defaults, these can be set in apache conf files
        environ.setdefault('dbgp.enabled', '')
        environ.setdefault('dbgp.idekey', '') # dbgp proxy support
        environ.setdefault('dbgp.host', '127.0.0.1')
        environ.setdefault('dbgp.port', 9000)

        enabled = environ.get('dbgp.enabled')
        if enabled:
            import dbgp.client
            idekey = environ.get('dbgp.idekey')
            host = environ.get('dbgp.host')
            port = environ.get('dbgp.port')
            client = dbgp.client.backendCmd(idekey)
            client.stdin_enabled = 0
            client.connect(host, port, 'application', [__file__])
   
            # despite it's name, this is how to run a function
            return client.runThread(self.__object, args, kwargs)
        else:
            # debugging disabled, continue normally
            return self.__object(*args, **kwargs)

application = Debugger(dispatch_request)

#!/usr/bin/python
# -*- coding: utf-8 -*-
# This is a simple mod_wsgi script for Trac
# See also: 
#   http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac
#   http://trac.edgewall.org/wiki/TracModWSGI
#
#   http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
#   this should be run via httpd -x

import sys
sys.stdout = sys.stderr

from trac.web.main import dispatch_request

class Debugger:

    def __init__(self, object):
        self.__object = object

    def __call__(self, *args, **kwargs):
        environ = args[0]
        # set dbgp defaults, these can be set in apache conf files
        environ.setdefault('dbgp.enabled', '') 
        environ.setdefault('dbgp.idekey', '') # dbgp proxy support
        environ.setdefault('dbgp.host', '127.0.0.1')
        environ.setdefault('dbgp.port', 9000)

        enabled = environ.get('dbgp.enabled')
        if enabled:
            import dbgp.client
            idekey = environ.get('dbgp.idekey')
            host = environ.get('dbgp.host')
            port = environ.get('dbgp.port')
            client = dbgp.client.backendCmd(idekey)
            client.stdin_enabled = 0
            client.connect(host, port, 'application', [__file__])
    
            # despite it's name, this is how to run a function
            return client.runThread(self.__object, args, kwargs)
        else:
            # debugging disabled, continue normally
            return self.__object(*args, **kwargs)

application = Debugger(dispatch_request)

riya123 | Sat, 2009-07-04 05:34

Thanks for informative post. I was nothing to known about how i can debug WSGI server based application on my laptop. Because it was creating so much errors. Know I have gotten your blog and feel satisfied that I can solve this problem now.