谷歌应用引擎应用错误:2 非数字端口:

1 投票
1 回答
603 浏览
提问于 2025-04-16 01:10

我在请求网址时,偶尔会遇到一个错误,提示“ApplicationError: 2 nonnumeric port: ''”,大约每十次请求中就有一次出现这个问题,其余的请求都正常。我发现这可能是个bug,但还没找到解决办法。有没有人知道为什么会出现这个情况?我使用的是Python 2.5.4和Google App Engine 1.3.3。

下面是一些通用的代码,错误是在随机请求页面时出现的。

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 个回答

1

你的代码里有几个地方可能会出问题。首先,你没有对传入的页面值做任何处理,但在你的try块里,这个值被fort thing的赋值给覆盖掉了。另外,正如我在评论中提到的,赋值中的%s需要一个变量来替换它的位置。你可能是想用传入的页面参数的值来替换这个%s。试试这个:

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 

编辑:

@user291071:我猜测通过overview的参数page传入的一些值可能没有以斜杠开头。确保URL解析器不会把附加的信息当作端口号的唯一方法是确保它以/或?开头。也许这样对你会更有效:

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

不过,这可能会导致其他当前正常工作的URL出现问题。最好的办法是记录下正在创建的URL,并逐一检查那些出错的URL。

撰写回答