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 a开发者_运维知识库m 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
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.
精彩评论