Search code examples
pythongoogle-app-enginehttplib

HTTPConnection .getresponse() throws an uncatchable error in my GAE app


I've got the following piece of code in a GAE app - there's very little to the app but this

class Check(webapp.RequestHandler):
    def get(self):
        sites = [    
            [1, 0, 'the google', 'www.google.com',  '555-0123']
        ]

        for site in sites:
            try:
                print "checking " + site[3] + "\r\n"
                conn = httplib.HTTPConnection(site[3])
                conn.request("GET", "/")
                r1 = conn.getresponse()
                print r1.status + "\r\n"
            except: # catch *all* exceptions
                e = sys.exc_info()[0]
                print e

This gives me the response:

Error: Server Error

The server encountered an error and could not complete your request.

Please try again in 30 seconds.

If I comment out the line print r1.status + "\r\n" the method executes just fine. Shouldn't except: catch this exception for me?

More importantly, why is it throwing an exception in the first place?


Solution

  • That is not an exception (as in communication transmission code exception), it's just a server response, perfectly valid from the communication transmission prospective.

    You need to address that differently, at the communication protocol level, above the communication transmission level.

    Update: The above note stands for the response you mentioned, which misled me.

    How exactly you're getting it I'm not sure, but the reason for the exception is your print line:

      File "blah.py", line 187, in post
        print r1.status + "\r\n"
    TypeError: unsupported operand type(s) for +: 'long' and 'str'
    

    You could replace the line with something like this to avoid the exception:

    print "%s\r\n" % r1.status