SyntaxError in Python handler
Question: I am using urlfetch available from Google App Engine to fetch URLs content.
But I am getting 500 Internal Server Error.

Here is the complete app code that I am using :-

**compare-hatke.py**

    
    
    import urllib2
    from google.appengine.api import urlfetch
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    import datetime
    
    class MainPage(webapp.RequestHandler):
        def curlTry:
            url = "http://www.google.com/"
            result = urlfetch.fetch(url)
            if result.status_code == 200:
                print(result.content)
    
    application = webapp.WSGIApplication([('/', MainPage)],debug=True)
    
    
    def main():
        run_wsgi_app(application)
    
    if __name__ == '__main__':
        main()
    

**app.yaml**

    
    
    application: compare-hatke
    version: 3
    runtime: python27
    api_version: 1
    threadsafe: false
    
    handlers:
    - url: /.*
      script: compare-hatke.app
    

Here is the **error log**. I am not able to understand the syntax error they
are mentioning

    
    
      Traceback (most recent call last):
      File "/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 196, in Handle
        handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
      File "/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 269, in _LoadHandler
        raise ImportError('%s has no attribute %s' % (handler, name))
    ImportError: <module 'compare-hatke' from '/base/data/home/apps/s~compare-hatke/3.365290288779373200/compare-hatke.pyc'> has no attribute app
    

Please tell me where I am missing. Thanks !


Answer: A Python method in a class needs to refer to the self parameter in the
function definition in order to identify the instance that it belongs to. This
is your basic syntax error.

Correcting that, you still need to set up a routing so that your MainPage
class can handle the GET request. This is most easily done by having a get
method in GAE. The documentation shows you other methods once you have this
working.

Try this:

    
    
    class MainPage(webapp.RequestHandler):
        def get(self): # responds to http GET, and adds self parameter
            url = "http://www.google.com/"
            result = urlfetch.fetch(url)
            if result.status_code == 200:
                print(result.content)
    

