teddy browns
teddy browns

Reputation: 141

CherryPy: 405 Method Not Allowed Specified method is invalid for this resource

I'm building my first web app with authentication in cherrypy: the auth piece works, but after the login I get the error 405 Method Not Allowed Specified method is invalid for this resource. Any idea on how to overcome it?

Thanks in advance!

from cherrypy.lib import auth_digest
import cherrypy

USERS = {'jon': 'secret'}

config = {
  'global' : {
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
    'server.thread_pool' : 8,
    'log.screen'         : True
  },
  '/' : {
    # HTTP verb dispatcher
    'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    # JSON response
    'tools.json_out.on' : True,
    # Digest Auth
    'tools.auth_digest.on'      : True,
    'tools.auth_digest.realm'   : 'walledgarden',
    'tools.auth_digest.get_ha1' : auth_digest.get_ha1_dict_plain(USERS),
    'tools.auth_digest.key'     : 'generate_something_random',
  }
}

class HelloWorld():
    def index(self):
        return "Hello World!"
    index.exposed = True

#cherrypy.quickstart(HelloWorld(), config=None) #this works
cherrypy.quickstart(HelloWorld(), config=config) #this is broken

enter image description here

Upvotes: 2

Views: 1958

Answers (2)

teddy browns
teddy browns

Reputation: 141

Thanks to cyraxjoe pointers I figured it out:

from cherrypy.lib import auth_digest
import cherrypy

USERS = {'jon': 'secret'}

config = {'/': {'tools.auth_digest.on': True,
               'tools.auth_digest.realm': 'walledgarden',
               'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(USERS),
               'tools.auth_digest.key': 'generate_something_random',
}}

class HelloWorld():
    def index(self):
        return "Hello World!"
    index.exposed = True

#cherrypy.quickstart(HelloWorld(), config=None) #this works -- NO AUTHENTICATION
cherrypy.quickstart(HelloWorld(), config=config) #WORKS WITH AUTHENTICATION

Upvotes: 1

cyraxjoe
cyraxjoe

Reputation: 5741

You are using the MethodDispacher which maps the HTTP methods as methods of the exposed class, in this case your index method should be called GET.

@cherrypy.expose
class HelloWorld():

    def GET(self):
        return "Hello World!"

If you are using a version of python that doesn't support class decorators, then use:

class HelloWorld():
    exposed = True

    def GET(self):
        return "Hello World!"

Basically, with the MethodDispatcher you expose resources (objects) that has methods matching the HTTP method, like GET - > def GET(self) or POST -> def POST(self).

You might find this post informative: https://blog.joel.mx/posts/cherrypy-101-method-dispatcher

Upvotes: 2

Related Questions