Re: 'Toolbox' object has no attribute 'CORS'

Kearney Taaffe <[email protected]>
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
Hey, try this: This is a server I wrote in CherryPy it should accept CORS 
requests. It's been working for me

===

import cherrypy

from sys import path
from os import getcwd 


# assumes there's a controller's module and a file name UsersController.py

from controllers.UsersController import Users

def CORS():
    """Allow AngularJS apps not on the same server to use our API
    """
    cherrypy.response.headers["Access-Control-Allow-Origin"] = "*" 
    cherrypy.response.headers["Access-Control-Allow-Headers"] = \
    "content-type, Authorization, X-Requested-With"
    cherrypy.response.headers["Access-Control-Allow-Methods"] = 'GET, POST'

if __name__ == '__main__':
    """Starts a cherryPy server and listens for requests
    """

    userController  = Users()
    
    cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)

    cherrypy.config.update({
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8080,
        'tools.CORS.on': True,
    })

    # API method dispatcher
    # we are defining this here because we want to map the HTTP verb to
    # the same method on the controller class. This _api_user_conf will
    # be used on each route we want to be RESTful
    _api_conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        }
    }

 

    cherrypy.tree.mount(userController, '/api/users', _api_conf)
    
    cherrypy.engine.start()
    cherrypy.engine.block()






On Tuesday, March 29, 2016 at 10:29:36 AM UTC-5, Jaakko Lappalainen wrote:
>
> I followed this post and I keep getting the 415 HTTP error. My origin is 
> still not allowed by cherryPy
>
> On Tuesday, March 29, 2016 at 4:49:58 AM UTC+1, Kearney Taaffe wrote:
>>
>> @morecowbell
>> I installed cherrypy-cors on my system, and changed the server.py file to 
>> look like this
>>
>> import os
>> import cherrypy 
>>
>> *import cherrypy_cors*
>>
>>
>> from cherrypy.lib.static import serve_file
>>
>> class Root(object):
>>    pass
>>   
>> if __name__ == '__main__':
>>     root = os.path.dirname(os.path.abspath(__file__))  # this file's 
>> directory 
>>
>> *    cherrypy_cors.install()*
>>
>>     
>>
>>     cherrypy.config.update( {
>>         'server.socket_host': '0.0.0.0',
>>         'server.socket_port': 8010,
>>     } )
>>
>>     conf = {
>>         '/': {
>>             'tools.staticdir.on': True,
>>             'tools.staticdir.root' : root,
>>             'tools.staticdir.dir': root + '/public/',
>>             'tools.staticdir.index': root + '/public/app/views/index.html'
>> ,
>>             *'cors.expose.on': True*
>>         }
>>     }
>>
>>  
>>
>>     cherrypy.quickstart(Root(), '/', conf)
>>
>>  
>> This is working perfectly for me now! Thank you so much! I posted the 
>> solution here so others who see this can know what I did to fix the 
>> problem. 
>>
>> Thanks again! 
>>
>>  
>>
>> On Wednesday, March 23, 2016 at 1:52:15 PM UTC-5, morecowbell wrote:
>>>
>>> assuming cherrypy-cors is installed, you're seemingly missing the module 
>>> import and setup.
>>>
>>>
>>> On Tuesday, March 22, 2016 at 8:38:37 PM UTC-7, Kearney Taaffe wrote:
>>>>
>>>> I've created an AngularJS web app, being served by a CherryPy server. 
>>>> It's calling an API which returns a JSON response
>>>>
>>>> The server.py (of my web application, not the API server) file looks 
>>>> like this
>>>>
>>>> import os
>>>> import cherrypy
>>>>
>>>> from cherrypy.lib.static import serve_file
>>>>
>>>> class Root(object):
>>>>    pass
>>>>   
>>>> if __name__ == '__main__':
>>>>     root = os.path.dirname(os.path.abspath(__file__))  # this file's 
>>>> directory
>>>>
>>>>     cherrypy.config.update( {
>>>>         'server.socket_host': '0.0.0.0',
>>>>         'server.socket_port': 8010,
>>>>     } )
>>>>
>>>>     conf = {
>>>>         '/': {
>>>>             'tools.staticdir.on': True,
>>>>             'tools.staticdir.root' : root,
>>>>             'tools.staticdir.dir': root + '/public/',
>>>>             'tools.staticdir.index': root + 
>>>> '/public/app/views/index.html',
>>>>             'tools.CORS.on': True
>>>>         }
>>>>     }
>>>>
>>>>  
>>>>
>>>>     cherrypy.quickstart(Root(), '/', conf)
>>>>
>>>>
>>>> However, when my angular app tries to hit the URI I get the following 
>>>> error
>>>>
>>>> [22/Mar/2016:15:28:28] ENGINE Bus STARTING
>>>> CherryPy Checker:
>>>> The config entry 'tools.CORS.on' may be invalid, because the 'CORS' 
>>>> tool was not found.
>>>> section: [/]
>>>>
>>>>  
>>>>
>>>> AttributeError: 'Toolbox' object has no attribute 'CORS'
>>>>
>>>>
>>>> Any clue as to what I'm doing wrong? 
>>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.