views:

51

answers:

1

I am getting the ApplicationError: 2 nonnumeric port: '' randomly for about 1/10th of my url request, the rest work fine, I seen this is a bug but I have yet to find any solutions, anyone have any thoughts in why this is occurring? I am running python 2.5.4 and google app engine 1.3.3

here is some generic code the error is occuring when requesting pages randomly

def overview(page):
     try:
          page = "http://www.url.com%s.json?" %page
          u = urllib.urlopen(page.encode('utf-8'))
          bytes = StringIO(u.read())
          u.close()
     except Exception, e:
          print e
          return None
     try:
        JSON_data = json.load(bytes)
        return JSON_data
     except ValueError:
        print "Couldn't get .json for %s" % page
        return None  
+1  A: 

Couple of things with your code that could be problems. One is that you aren't doing anything with the incoming value of page, but it is being over-written by the assignment fort thing inside your try block. Also, as I noted in my comment, the %s in the assignment wants to have a variable to substitute in its place. That's probably what you are meant to to with the value coming in the page parameter. Try this:

def overview(page_to_get):
     try:
          page = "http://www.url.com%s.json?" % page_to_get
          u = urllib.urlopen(page.encode('utf-8'))
          bytes = StringIO(u.read())
          u.close()
     except Exception, e:
          print e
          return None
     try:
        JSON_data = json.load(bytes)
        return JSON_data
     except ValueError:
        print "Couldn't get .json for %s" % page
        return None 

EDIT:

@user291071: I would guess that some of that values that are coming in through overview's parameter page do not start with a leading slash. The only way to make sure that the URL parser doesn't try to interpret the added-on information as a port-number is to make sure that it starts with a / or a ?. perhaps this will work better for you:

          page = "http://www.url.com/%s.json?" % page_to_get

but it may cause other URLs that are currently working to fail. The best thing to do would be to log the URLs that are being created and visually inspect the ones that are failing.

Adam Crossland
sorry my code might not be clear the page or page_to_get would be the extension onto url.com eg url.com/random/bla/1 , basically same thing you did above except i just reassigned. I know my urls are correct but they are randomly just getting the port errors when going to retrieve the page. Plus when running all I can see when done is ApplicationError: 2 nonnumeric port: ‘’ because its in sdk. I havn't been able to find what causes a nonnumeric port error yet.
Hi adam spot on advice all around most of the errors you pointed out were things i've already dealt with, I did find the the error, was in the pages I was feeding sometime had odd characters and so it was the urls basically being wrong feeding into the function. thx for bouncing ideas off I went back and was looking at the urls
Glad to hear it, user!
Adam Crossland