Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

404 error With tornado module and CORS

Hello Treehouse community ,

i follow the tutorial based on the service i wan't to use ( Drone Deploy) : https://dronedeploy.gitbooks.io/dronedeploy-apps/content/server_example.html i made a topic on their forum for this issue today.

But maybe someone can help me here :)

I got an issue with an Heroku server app i do. i use Python and the Tornado module.

The fact is i got a 404 error My terminal ( when test it localy ) ask me : "WARNING:tornado.access:404 GET / (::1) 0.75ms"

so the "GET" function is not working , maybe because of HTTP access control (CORS) i try many fix , but none worked.

Maybe i made something wrong ,or forgot something.

this the code :

import os
import requests

import tornado.ioloop

import tornado.web


GEOCODE_FMT = 'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={key}'


class GeocodeHandler(tornado.web.RequestHandler):
    """Proxy a call to the Google Maps geocode API"""

    def set_default_headers(self):
        # allow cross-origin requests to be made from your app on DroneDeploy to your web server
        self.set_header("Access-Control-Allow-Origin", "https://www.dronedeploy.com")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        # add more allowed methods when adding more handlers (POST, PUT, etc.)
        self.set_header("Access-Control-Allow-Methods", "GET, OPTIONS")

    def get(self):
        api_key = os.environ.get("MyApiKey")
        address = self.get_query_argument("address")
        url = GEOCODE_FMT.format(address=address, key=api_key)

        # fetch results of the geocode from Google
        response = requests.get(url)

        # send the results back to the client
        self.write(response.content)

    def options(self):
        # no body
        self.set_status(204)
        self.finish()


def main():
    application = tornado.web.Application([
        (r"/geocode/", GeocodeHandler)
    ])
    port = int(os.environ.get("PORT", 5000))
    application.listen(port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

Any help, advices are appreciated

Thanks

John

[MOD: added ```python formatting -cf]

1 Answer

hi , some news .

Someone of the Drone deploy said my tornado routes aren't being picked up by heroku. After following many tutorial and read the Tornado documentation i have no idea how to routes this.

help really appreciated :(