当 json 响应指示无效请求时,以下代码会表现出奇怪的行为。代码不会记录响应然后点击“if”块,而是记录响应并立即再次跳转到第一个日志调用,并重试 json/urlopen 调用等。此过程将无限重复,直到我终止该过程. 请注意,当 json 响应表明 url 和查询有效且正常时,程序的流程与您预期的一样。
log.debug('Retreiving json response for %s' % url)
response = simplejson.load(urllib2.urlopen(url))
log.debug('Retreived json response: %s' % response)
if response['status'] in ['INVALID_REQUEST', 'REQUEST_DENIED', 'UNKNOWN_ERROR']:
print 'Problem with request: %s' % response['status']
只是为了演示这个问题,我已经包含了一个生成日志的示例:
2012-12-26 09:41:31,505 - json_logger - DEBUG - Retreiving json response for ...
2012-12-26 09:41:31,510 - json_logger - DEBUG - Retreived json response: {'status': 'INVALID_REQUEST', 'results': []}
2012-12-26 09:41:31,512 - json_logger - DEBUG - Retreiving json response for ...
2012-12-26 09:41:31,530 - json_logger - DEBUG - Retreived json response: {'status': 'INVALID_REQUEST', 'results': []} ...