Still need help with the 405....please

81 views
Skip to first unread message

Jeff Silverman

unread,
Jul 3, 2012, 9:32:20 AM7/3/12
to Django users
Below is the code from the views.py

The 405 is retunred from the 'return super(DjangoSoapApp,
self).__init__(Application(services, tns))' statement. I am using
python 2.6, soaplib20 and django 1.3. I am struggling to understand
what exactly is wrong here.



class HelloWorldService(DefinitionBase):
@soap(String,Integer,_returns=Array(String))
def say_smello(self,name,times):
results = []
for i in range(0,times):
results.append('Hello, %s'%name)
return results

class DjangoSoapApp(WSGIApplication):
csrf_exempt = True

def __init__(self, services, tns):
"""Create Django view for given SOAP soaplib services and
tns"""

return super(DjangoSoapApp,
self).__init__(Application(services, tns))

def __call__(self, request):
django_response = HttpResponse()

def start_response(status, headers):
django_response.status_code = int(status.split(' ', 1)[0])
for header, value in headers:
django_response[header] = value

response = super(DjangoSoapApp, self).__call__(request.META,
start_response)
django_response.content = '\n'.join(response)

return django_response

# the view to use in urls.py
hello_world_service = DjangoSoapApp([HelloWorldService], '__name__')

Nikolas Stevenson-Molnar

unread,
Jul 3, 2012, 1:28:20 PM7/3/12
to django...@googlegroups.com
I'm not sure that this is the problem, but typically constructors should
not have a return value. Try removing the "return" from your
DjangoSoapApp constructor.

_Nik

Jeff Silverman

unread,
Jul 3, 2012, 2:33:55 PM7/3/12
to Django users
Thanks for the reply. Removing that did not change the result. Just
an FYI, but I copied the code verbatim from the snippet. that's why I
cannot understand what's going on. I continually get the 405 method
not allowed error regardless.

On Jul 3, 1:28 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> > hello_world_service = DjangoSoapApp([HelloWorldService], '__name__')- Hide quoted text -
>
> - Show quoted text -

Nikolas Stevenson-Molnar

unread,
Jul 3, 2012, 2:56:45 PM7/3/12
to django...@googlegroups.com
Would you please provide a reference to the snippet or to your complete
code? It's hard to understand what's going on from this small bit.

_Nik

Jeff Silverman

unread,
Jul 3, 2012, 3:20:03 PM7/3/12
to Django users
http://djangosnippets.org/snippets/2638/

On Jul 3, 2:56 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> >> - Show quoted text -- Hide quoted text -

Nikolas Stevenson-Molnar

unread,
Jul 3, 2012, 3:47:19 PM7/3/12
to django...@googlegroups.com
Looking at the soaplib source, it looks like it required requests to be
made using POST. If you're loading this in a web browser to test, then
you're making a GET request. Try making a POST request (using something
like Fiddler) instead.

https://github.com/soaplib/soaplib/blob/master/src/soaplib/core/server/wsgi.py
(line 84/85)

_Nik

Jeff Silverman

unread,
Jul 5, 2012, 7:26:52 AM7/5/12
to Django users
Good call. I used Fiddler to generate the POST string, however, I get
no response. It appears to go into the @soap(String, Integer,
_returns=Array(string)) statement and hangs at that point, not
returning.

On Jul 3, 3:47 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Looking at the soaplib source, it looks like it required requests to be
> made using POST. If you're loading this in a web browser to test, then
> you're making a GET request. Try making a POST request (using something
> like Fiddler) instead.
>
> https://github.com/soaplib/soaplib/blob/master/src/soaplib/core/serve...
> (line 84/85)
>
> _Nik
>
> On 7/3/2012 12:20 PM, Jeff Silverman wrote:
>
>
>
> >http://djangosnippets.org/snippets/2638/
>
> > On Jul 3, 2:56 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> > wrote:
> >> Would you please provide a reference to the snippet or to your complete
> >> code? It's hard to understand what's going on from this small bit.
>
> >> _Nik
>
> >> On 7/3/2012 11:33 AM, Jeff Silverman wrote:
>
> >>> Thanks for the reply.  Removing that did not change the result.  Just
> >>> an FYI, but I copied the code verbatim from the snippet.  that's why I
> >>> cannot understand what's going on.  I continually get the405method
> >>> not allowed error regardless.
> >>> On Jul 3, 1:28 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> >>> wrote:
> >>>> I'm not sure that this is the problem, but typically constructors should
> >>>> not have a return value. Try removing the "return" from your
> >>>> DjangoSoapApp constructor.
> >>>> _Nik
> >>>> On 7/3/2012 6:32 AM, Jeff Silverman wrote:
> >>>>> Below is the code from the views.py
> >>>>> The405is retunred from the 'return super(DjangoSoapApp,

Jeff Silverman

unread,
Jul 5, 2012, 8:20:06 AM7/5/12
to Django users
Ok, I'm further along, I think. Now I'm getting the following

response = super(DjangoSoapApp, self).__call__(environ,
start_response)
(Pdb) p start_response
<function start_response at 0x25d1ed8>

(Pdb) super(DjangoSoapApp, self).__call__(environ, start_response)
*** TypeError: super() argument 1 must be type, not function


On Jul 3, 3:47 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Looking at the soaplib source, it looks like it required requests to be
> made using POST. If you're loading this in a web browser to test, then
> you're making a GET request. Try making a POST request (using something
> like Fiddler) instead.
>
> https://github.com/soaplib/soaplib/blob/master/src/soaplib/core/serve...
> (line 84/85)
>
> _Nik
>
> On 7/3/2012 12:20 PM, Jeff Silverman wrote:
>
>
>
> >http://djangosnippets.org/snippets/2638/
>
> > On Jul 3, 2:56 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> > wrote:
> >> Would you please provide a reference to the snippet or to your complete
> >> code? It's hard to understand what's going on from this small bit.
>
> >> _Nik
>
> >> On 7/3/2012 11:33 AM, Jeff Silverman wrote:
>
> >>> Thanks for the reply.  Removing that did not change the result.  Just
> >>> an FYI, but I copied the code verbatim from the snippet.  that's why I
> >>> cannot understand what's going on.  I continually get the405method
> >>> not allowed error regardless.
> >>> On Jul 3, 1:28 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> >>> wrote:
> >>>> I'm not sure that this is the problem, but typically constructors should
> >>>> not have a return value. Try removing the "return" from your
> >>>> DjangoSoapApp constructor.
> >>>> _Nik
> >>>> On 7/3/2012 6:32 AM, Jeff Silverman wrote:
> >>>>> Below is the code from the views.py
> >>>>> The405is retunred from the 'return super(DjangoSoapApp,

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 1:38:13 PM7/5/12
to django...@googlegroups.com
Hmmm, not sure about this one. Try printing out the type of
DjangoSoapApp before that line is called:

print type(DjangoSoapApp)

_Nik

Jeff Silverman

unread,
Jul 5, 2012, 2:01:20 PM7/5/12
to Django users
The print output is:

<type 'function'>


On Jul 5, 1:38 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 2:03:05 PM7/5/12
to django...@googlegroups.com
Is your code still the same as you posted earlier:
http://djangosnippets.org/snippets/2638/? And the error is occuring on
ln 28?

_Nik

Jeff Silverman

unread,
Jul 5, 2012, 2:17:56 PM7/5/12
to Django users
I've been flip flopping my views.py between that snippet, and
https://gist.github.com/935809, which is a bit different, but easier
to follow.

On Jul 5, 2:03 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Is your code still the same as you posted earlier:http://djangosnippets.org/snippets/2638/?And the error is occuring on

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 2:31:38 PM7/5/12
to django...@googlegroups.com
Hmmm, I can't think of what may be happening. One more debug thing to
try, print the help of DjangoSoapApp just before the problem line:

print help(DjangoSoapApp)

That way, if the DjangoSoapApp symbol is getting reassigned to a
function somewhere along the way, that might clue you in.

_Nik

Jeff Silverman

unread,
Jul 5, 2012, 2:37:09 PM7/5/12
to Django users
Resulting output,

Help on function DjangoSoapApp in module mysite.BDSCheckUser.views:

DjangoSoapApp(*args, **kwargs)


On Jul 5, 2:31 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Hmmm, I can't think of what may be happening. One more debug thing to
> try, print the help of DjangoSoapApp just before the problem line:
>
> print help(DjangoSoapApp)
>
> That way, if the DjangoSoapApp symbol is getting reassigned to a
> function somewhere along the way, that might clue you in.
>
> _Nik
>
> On 7/5/2012 11:17 AM, Jeff Silverman wrote:
>
>
>
> > I've been flip flopping my views.py between that snippet, and
> >https://gist.github.com/935809, which is a bit different, but easier
> > to follow.
>
> > On Jul 5, 2:03 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> > wrote:
> >> Is your code still the same as you posted earlier:http://djangosnippets.org/snippets/2638/?Andthe error is occuring on

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 2:54:12 PM7/5/12
to django...@googlegroups.com
Would you please provide the source for mysite.BDSCheckUser.views?

_Nik

Jeff Silverman

unread,
Jul 5, 2012, 4:03:05 PM7/5/12
to Django users
# soaplib v2.0.0beta2 (from memory)
# Django v1.3 (stable)
# NOTE: CSRF middleware has been turned off!
# For urls.py, see: https://gist.github.com/935812

import soaplib
from soaplib.core.service import rpc, DefinitionBase
from soaplib.core.model.primitive import String, Integer
from soaplib.core.model.clazz import Array

from django.views.decorators.csrf import csrf_exempt


class HelloWorldService(DefinitionBase):
@rpc(String,Integer,_returns=Array(String))
def say_hello(self, name, times):
results = []
for i in range(0, times):
results.append('Hellow, %s' %name)
return results


from soaplib.core.server.wsgi import Application
from django.http import HttpResponse

import StringIO
class DumbStringIO(StringIO.StringIO):
def read(self, n):
return self.getvalue()

@csrf_exempt
class DjangoSoapApp(Application):
def __call__(self, request):
django_response = HttpResponse()

def start_response(status, headers):
status, reason = status.split(' ', 1)
django_response.status_code = int(status)
for header, value in headers:
django_response[header] = value

environ = request.META.copy()
environ['CONTENT_LENGTH'] = len(request.raw_post_data)
environ['wsgi.input'] = DumbStringIO(request.raw_post_data)
environ['wsgi.multithread'] = False

# print help(DjangoSoapApp)

response = super(DjangoSoapApp, self).__call__(environ,
start_response)
django_response.content = '\n'.join(response)
return django_response


print type(DjangoSoapApp)
soap_application = soaplib.core.Application([HelloWorldService],
'tns')
#import pdb; pdb.set_trace()
hello_world_service = DjangoSoapApp(soap_application)


On Jul 5, 2:54 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Would you please provide the source for mysite.BDSCheckUser.views?
>
> _Nik
>
> On 7/5/2012 11:37 AM, Jeff Silverman wrote:
>
>
>
> > Resulting output,
>
> > Help on function DjangoSoapApp in module mysite.BDSCheckUser.views:
>
> > DjangoSoapApp(*args, **kwargs)
>
> > On Jul 5, 2:31 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> > wrote:
> >> Hmmm, I can't think of what may be happening. One more debug thing to
> >> try, print the help of DjangoSoapApp just before the problem line:
>
> >> print help(DjangoSoapApp)
>
> >> That way, if the DjangoSoapApp symbol is getting reassigned to a
> >> function somewhere along the way, that might clue you in.
>
> >> _Nik
>
> >> On 7/5/2012 11:17 AM, Jeff Silverman wrote:
>
> >>> I've been flip flopping my views.py between that snippet, and
> >>>https://gist.github.com/935809, which is a bit different, but easier
> >>> to follow.
> >>> On Jul 5, 2:03 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> >>> wrote:
> >>>> Is your code still the same as you posted earlier:http://djangosnippets.org/snippets/2638/?Andtheerror is occuring on

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 5:00:47 PM7/5/12
to django...@googlegroups.com
Try removing the @csrf_exempt decorator (for testing, you can disable
CSRF for the site in your settings file by commenting out the
CsrfViewMiddleware).

_Nik

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 5:11:20 PM7/5/12
to django...@googlegroups.com
Here's a good example of how Python decorators work behind the scenes:
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators.
Essentially, the @csrf_exempt decorator is a function, meaning that when
you use it to decorate a class, you reassign the name of that class to a
function which returns a class instance. This creates problems when you
use super() or anything else that expects a class (and gets a function
instead).

This is the correct way to decorate a class-based view:
https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-the-class.
Except I don't think that DjangoSoapApp is a view (?) so you'll need to
find where the actual view is and decorate that instead.

_Nik

Jeff Silverman

unread,
Jul 5, 2012, 8:33:51 PM7/5/12
to django...@googlegroups.com
Nik, I will give that a try.  The reason for the decorator was that I was getting 403 forbidden, and the decorator made that one go away.  If I remove the csrf from the settings file, will that solve that problem?

Jeff Silverman

unread,
Jul 5, 2012, 9:22:29 PM7/5/12
to django...@googlegroups.com
Nik, if I remove the csrf decorator and leave the middleware in place, I get the 403.  Is there a way to add the token on the POST command, or is there another way of leaving the middleware in place, but turn off csrf without using the decorator?

Nikolas Stevenson-Molnar

unread,
Jul 5, 2012, 10:35:33 PM7/5/12
to django...@googlegroups.com
Yes, I would expect a 403 when the CSRF middleware is active, the decorator is not used, and no CSRF token is provided. This is the intended behavior. You can fix this in a few ways:

  1. Apply the decorator to the __call__ method (rather than to the class itself). If I understand how this code works, that should correctly disable CSRF for the view.
  2. Provide a CSRF value with the POST data, as you suggested. This all depends on how the request is made. Django's CSRF system relies on a CSRF value set in a cookie. You have to mimic a browsers cookie functionality, then use the value of the CSRF cookie with evey request you make. By default, the cookie name is 'csrftoken'. For more info on the CSRF process: https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/
  3. Disable CSRF altogether. Simply remove the CsrfViewMiddleware from your settings and you're good to go.

_Nik

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/WpDQ4UjGEQwJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Jeff Silverman

unread,
Jul 6, 2012, 9:03:55 AM7/6/12
to Django users
Ok Nik. I have removed the CSRF middleware and get a brand new error.

XMLSyntaxError at /hello/
error parsing attribute name, line 1, column 6Request Method: POST
Request URL: http://piadm42.troweprice.com:5555/hello/
Django Version: 1.3.1
Exception Type: XMLSyntaxError
Exception Value: error parsing attribute name, line 1, column 6
Exception Location: /usr/lib/python2.6/site-packages/
soaplib-2.0.0_beta1-py2.6.egg/soaplib/core/_base.py in
_parse_xml_string, line 248
Python Executable: /usr/bin/python
Python Version: 2.6.6

The POST in fiddler is something like this -> http://mysite.com:5555/hello/
and the request body contains -> <name=fred&times=2>



On Jul 5, 10:35 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Yes, I would expect a 403 when the CSRF middleware is active, the
> decorator is not used, and no CSRF token is provided. This is the
> intended behavior. You can fix this in a few ways:
>
>  1. Apply the decorator to the __call__ method (rather than to the class
>     itself). If I understand how this code works, that should correctly
>     disable CSRF for the view.
>  2. Provide a CSRF value with the POST data, as you suggested. This all
>     depends on how the request is made. Django's CSRF system relies on a
>     CSRF value set in a cookie. You have to mimic a browsers cookie
>     functionality, then use the value of the CSRF cookie with evey
>     request you make. By default, the cookie name is 'csrftoken'. For
>     more info on the CSRF process:
>    https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/
>  3. Disable CSRF altogether. Simply remove the CsrfViewMiddleware from
> >http://groups.google.com/group/django-users?hl=en.- Hide quoted text -

Nikolas Stevenson-Molnar

unread,
Jul 6, 2012, 12:03:21 PM7/6/12
to django...@googlegroups.com
Perhaps it's expecting XML in the request and trying to parse your POST data (which is not XML)?

_Nik

Jeff Silverman

unread,
Jul 6, 2012, 1:32:19 PM7/6/12
to Django users
What should the XML look like?

On Jul 6, 12:03 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
> >>>         The405is retunred from the 'return super(DjangoSoapApp,
> >>>http://groups.google.com/group/django-users?hl=en.-Hide quoted text -
>
> >> - Show quoted text -
>
> > --
> > You received this message because you are subscribed to the Google Groups "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to django-users...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.- Hide quoted text -

Nikolas Stevenson-Molnar

unread,
Jul 6, 2012, 1:40:05 PM7/6/12
to django...@googlegroups.com
I'm not exactly sure, I usually try to avoid SOAP. But perhaps something
like this? http://www.w3schools.com/soap/soap_example.asp. You'll have
to modify it to match your SOAP app, of course.

Depending on what you're trying to accomplish, SOAP may not be your best
option. I tend to prefer REST, and there are some good Django-compatible
libraries for implementing REST interfaces:
http://django-rest-framework.org/ (for example; there are others as well)

_Nik

Jeff Silverman

unread,
Jul 6, 2012, 3:44:47 PM7/6/12
to Django users
Nik, I appreciate your help, but I'm about at the end with this. I'm
throwing in the towel.

On Jul 6, 1:40 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> I'm not exactly sure, I usually try to avoid SOAP. But perhaps something
> like this?http://www.w3schools.com/soap/soap_example.asp. You'll have
> to modify it to match your SOAP app, of course.
>
> Depending on what you're trying to accomplish, SOAP may not be your best
> option. I tend to prefer REST, and there are some good Django-compatible
> libraries for implementing REST interfaces:http://django-rest-framework.org/(for example; there are others as well)
> >>>>>http://groups.google.com/group/django-users?hl=en.-Hidequoted text -
> >>>> - Show quoted text -
> >>> --
> >>> You received this message because you are subscribed to the Google Groups "Django users" group.
> >>> To post to this group, send email to django...@googlegroups.com.
> >>> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> >>> For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.-Hide quoted text -
> >> - Show quoted text -- Hide quoted text -

Nikolas Stevenson-Molnar

unread,
Jul 6, 2012, 3:58:56 PM7/6/12
to django...@googlegroups.com
Out of curiosity, what is your end-goal with this?

_Nik

Jeff Silverman

unread,
Jul 6, 2012, 5:59:07 PM7/6/12
to django...@googlegroups.com
Nik I was trying to figure out how to make web services on such a way that I could import the wsdl into an info path form eventually pulling data from f5 ltm devices.  Sort of ambitious I think but djamgo is supposed to be easy. 

Not. 
Reply all
Reply to author
Forward
0 new messages