Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Ive got a Django web application setup with mod_wsgi and apache2 on ubuntu 9.10

Im using the following code to o a user authentication via a HTTP post for another part of my site. All it required back is a HTTP 200.

from django.contrib.auth import authenticate
from django.http import HttpResponse


def post_authentication_api(request):
    if request.method == 'POST':
        print request
        user = authenticate(username=request.POST['user'], password=request.POST['pass'])
        if user is not None:
            if user.is_active:
                print "correct"
                return HttpResponse("correct", mimetype="text/plain")
            else:
                print "disabled"
                return HttpResponse("disabled", mimetype="text/plain", status=401)
        else:
            print "incorrect"
            return HttpResponse("incorrect", mimetype="text/plain", status=401)

This all works fine when I run it using python manage.py runserver command and the other application authenticates fine. However when i load it into apache i get a 500 internal error. In fact when I run the following python code

import urllib
import urllib2

url = 'http://192.168.0.5/radiobusi/auth/post_authentication_api'
values = {'user' : 'Michael Foord',
          'pass' : 'Northampton',}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

I get

Traceback (most recent call last):
  File "testURL.py", line 10, in <module>
    response = urllib2.urlopen(req)
  File "/usr/lib/python2.6/urllib2.py", line 124, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 395, in open
    response = meth(req, response)
  File "/usr/lib/python2.6/urllib2.py", line 508, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.6/urllib2.py", line 433, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.6/urllib2.py", line 367, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 516, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR

this is my virtual host file

<VirtualHost *:80>
    #Basic setup
    ServerAdmin munderwo@iinet.net.au
    ServerName 192.168.0.5 
    ServerAlias 192.168.0.5

    <Directory /home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/apache/>
        Order deny,allow
        Allow from all
    </Directory>

    Alias /radiobusi/media /home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/media

    <Location /media>
      Order allow,deny
      Allow from all
    </Location>

    LogLevel debug
    ErrorLog  /home/munderwo/public_html/radiobusi.com/logs/apache_error.log
    CustomLog /home/munderwo/public_html/radiobusi.com/logs/apache_access.log combined

    WSGIDaemonProcess radiobusi.com user=www-data group=www-data threads=25
    WSGIProcessGroup radiobusi.com

    WSGIScriptAlias /radiobusi /home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/apache/RadioBusi.wsgi
</VirtualHost>

and this is my wsgi file

import os
import sys

apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace) 

sys.path.append('/usr/local/lib/python2.6/dist-packages/Django-1.1.1-py2.6.egg/django/')
sys.path.append('/home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/')

os.environ['PYTHON_EGG_CACHE'] = '/home/munderwo/public_html/radiobusi.com/egg_cache'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

ive already set all permissions to 777 on the ~/public_html/radiobusi.com/ and turned loggin to debug on apache and the virtual host. Anybody got any other ideas what this could be?

Cheers

Mark.

Update:

The following is the log from the virtual hosts, both the error log and the access log

[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Attach interpreter ''.
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Enable deadlock thread in process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8668): mod_wsgi (pid=27874): Starting 25 threads in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 1 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Enable monitor thread in process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8508): mod_wsgi (pid=27874): Deadlock timeout is 300.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8511): mod_wsgi (pid=27874): Inactivity timeout is 0.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 2 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 3 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 4 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 5 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 6 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 7 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 8 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 9 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 10 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 11 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 12 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 13 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 14 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 15 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 16 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 17 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 18 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 19 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 20 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 21 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 22 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 23 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 24 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8674): mod_wsgi (pid=27874): Starting thread 25 in daemon process 'radiobusi.com'.
[Mon Jan 18 19:54:47 2010] [info] mod_wsgi (pid=27874): Create interpreter '192.168.0.5|/radiobusi'.
[Mon Jan 18 19:54:47 2010] [info] [client 192.168.0.5] mod_wsgi (pid=27874, process='radiobusi.com', application='192.168.0.5|/radiobusi'): Loading WSGI script '/home/munderwo/public_html/radiobusi.com/RadioBusiSite/mysite/apache/RadioBusi.wsgi'.
munderwo@bertha:~/public_html/radiobusi.com/logs$ cat apache_access.log 
192.168.0.5 - - [18/Jan/2010:19:54:47 +0800] "POST /radiobusi/auth/post_authentication_api HTTP/1.1" 500 42213 "-" "Icecast 2.3.2"
192.168.0.5 - - [18/Jan/2010:19:55:04 +0800] "POST /radiobusi/auth/post_authentication_api HTTP/1.1" 500 42213 "-" "Icecast 2.3.2"

to get these I stopped apache. removed the logs. touched them again. then restarted apache and tried the POST request twice.

this is from the main apache log file at /var/log/apache2/error.log. Ive included the message of it shutting own as a way of time tracking it.

   [Mon Jan 18 19:53:47 2010] [notice] caught SIGTERM, shutting down
    [Mon Jan 18 19:54:27 2010] [notice] mod_python: Creating 8 session mutexes based on 6 max processes and 25 max threads.
    [Mon Jan 18 19:54:27 2010] [notice] mod_python: using mutex_directory /tmp 
    [Mon Jan 18 19:54:27 2010] [warn] mod_wsgi: Compiled for Python/2.6.2.
    [Mon Jan 18 19:54:27 2010] [warn] mod_wsgi: Runtime using Python/2.6.4.
    [Mon Jan 18 19:54:27 2010] [warn] mod_wsgi: Python module path '/usr/lib/python2.6/:/usr/lib/python2.6/plat-linux2:/usr/lib/python2.6/lib-tk:/usr/lib/python2.6/lib-old:/usr/lib/python2.6/lib-dynload'.
    [Mon Jan 18 19:54:27 2010] [debug] mod_wsgi.c(8070): mod_wsgi (pid=27872): Socket for 'radiobusi.com' is '/var/run/apache2/wsgi.27872.0.1.sock'.
    [Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27874): Starting process 'radiobusi.com' with uid=33, gid=33 and threads=25.
    [Mon Jan 18 19:54:27 2010] [notice] Apache/2.2.12 (Ubuntu) DAV/2 SVN/1.6.5 mod_python/3.3.1 Python/2.6.4 mod_wsgi/2.5 configured -- resuming normal operations
    [Mon Jan 18 19:54:27 2010] [info] Server built: Nov 12 2009 22:50:52
    [Mon Jan 18 19:54:27 2010] [debug] worker.c(1740): AcceptMutex: sysvsem (default: sysvsem)
    [Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27877): Attach interpreter ''.
    [Mon Jan 18 19:54:27 2010] [info] mod_wsgi (pid=27878): Attach interpreter ''.

also in my testURL.py i believe I am printing out the response. well at least im trying to. with the lines

response = urllib2.urlopen(req)
the_page = response.read() 

is there a better way to do this?

Update: Added urls.py as per request in comments

from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib.auth import forms

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root),
    (r'^polls/', include('mysite.polls.urls')),
    (r'auth/post_authentication_api$', 'mysite.users.views.post_authentication_api'),
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/Users/munderwood/Documents/Aptana Studio Workspace/RadioBusiSite/mysite/media/'}),    
)

and as far as I can tell my apache2.conf does not have an ErrorDocument setup. its commented out.

ok Managed to get a django error page out of it. rather weird.

Environment:

Request Method: POST
Request URL: http://bertha.homeunix.org:3000/radiobusi/auth/post_authentication_api
Django Version: 1.1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.flatpages',
 'mysite.polls',
 'mysite.RadioBusi',
 'mysite.Support',
 'mysite.users']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/Django-1.1.1-py2.6.egg/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /auth/post_authentication_api
Exception Value: post_authentication_api() takes exactly 2 arguments (1 given)

POST
Variable    Value
user    u'e_username'
pass        u'e_password'

The stuff under POST is the post variables. hope this helps. Its obviously something to do with my setup. Its just weird that when i run the same code in the django test server it works, while as soon as i put it on the apache webserver it does this?

thanks again for your help!

Cheers

Mark

share|improve this question
1  
That traceback is for your client, what were the error messages in Apache error log if any. That or modify your client to dump out the content of the response and not just the error status as the content may contain more information if DEBUG is enabled on Django installation. –  Graham Dumpleton Jan 17 '10 at 21:59
    
add information to help with diagnosing the problem –  Mark Jan 18 '10 at 12:05
    
What is in your urls.py file? Does you Apache configuration have an ErrorDocument directive set for 404 error status? –  Graham Dumpleton Jan 18 '10 at 21:44
    
Did you modify client to dump out the content of the response. Ie., catch the HTTPError exception and access 'fp' member attribute, using it as file descriptor to read remainder of response including content of page. At think that is how you do it. –  Graham Dumpleton Jan 23 '10 at 5:47
    
Added what I think it relevant from the django error page that i got. hope this is enough.. –  Mark Jan 24 '10 at 2:11

1 Answer 1

up vote 1 down vote accepted

Ok, so once again we come down to user error. At some point the problem changed because I changed some of the code above, so that

def post_authentication_api(request): 

became

def post_authentication_api(self, request):

this doesnt work because as Graham said at the django mailing list

"If you ... have a 'self' first argument for a function which isn't a method of a class, it is obvious you are going to get:"

Exception Type: TypeError at /auth/post_authentication_api 
  Exception Value: post_authentication_api() takes exactly 2 arguments 
(1 given)

So after I fixed that up I came to the root of the problem (as far as I can tell) which was that wsgi was blocking sys.stdout. which I think seems fair to me, and I had some print statements in my code and this is why it wasnt running. A very simple problem that for some reason became very complicated.

Anyway, Great thanks to Graham who has helped me out for the last week on this. He has been tireless and very accommodating to someone who is quite embarrassed about the final outcome. Some votes will be coming his way for the comments and my many thanks as well.

Thanks again

Mark

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.