【已解决】python执行出错:HTTPError: HTTP Error 502: Bad Gateway
【背景】
某此脚本执行,出现错误:
LINE 1687 : ERROR Unknown Error ! Traceback (most recent call last): File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 1685, in <module> try: File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 1632, in main gVal[‘statInfoDict’][‘exportedItemIdx’] += 1 File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 1351, in exportEntry calcTimeStart(“translate_post_name”) File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 849, in translateString trans_req.add_header(‘User-Agent’, “Mozilla/4.0 (compatible;MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)”) File “E:dev_install_rootPython27liburllib2.py”, line 126, in urlopen return _opener.open(url, data, timeout) File “E:dev_install_rootPython27liburllib2.py”, line 400, in open response = meth(req, response) File “E:dev_install_rootPython27liburllib2.py”, line 513, in http_response ‘http’, request, response, code, msg, hdrs) File “E:dev_install_rootPython27liburllib2.py”, line 438, in error return self._call_chain(*args) File “E:dev_install_rootPython27liburllib2.py”, line 372, in _call_chain result = func(*args) File “E:dev_install_rootPython27liburllib2.py”, line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 502: Bad Gateway Traceback (most recent call last): File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 1685, in <module> try: File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 1632, in main gVal[‘statInfoDict’][‘exportedItemIdx’] += 1 File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 1351, in exportEntry calcTimeStart(“translate_post_name”) File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-24.py”, line 849, in translateString trans_req.add_header(‘User-Agent’, “Mozilla/4.0 (compatible;MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)”) File “E:dev_install_rootPython27liburllib2.py”, line 126, in urlopen return _opener.open(url, data, timeout) File “E:dev_install_rootPython27liburllib2.py”, line 400, in open response = meth(req, response) File “E:dev_install_rootPython27liburllib2.py”, line 513, in http_response ‘http’, request, response, code, msg, hdrs) File “E:dev_install_rootPython27liburllib2.py”, line 438, in error return self._call_chain(*args) File “E:dev_install_rootPython27liburllib2.py”, line 372, in _call_chain result = func(*args) File “E:dev_install_rootPython27liburllib2.py”, line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) |
暂时不知道是什么具体原因导致的。
虽然表面看起来是网络断了,但是好像网络是正常的,所以按理来说不应该出现这个错误才对。
后来又重新运行了一次,就没出现这个错误。
【后记 2011-12-25】
后来去看了代码,才发现,其实这里的错误,是有缘由的。
那就是,上述代码中调用了translateString函数,函数内部,调用了google的翻译功能,即向google发送请求然后获得翻译后的结果。
此翻译功能,google方面是有限制的,在一定时间内,超过一定请求,就会暂时对该ip停止此功能。
此处的错误,就是这个原因引起的,即在短时间内,发起了太多的翻译请求,所以google就会暂时停止提供此服务,导致HTTPError: HTTP Error 502: Bad Gateway的错误。
【解决办法】
解决办法很简单,那就是程序实现翻译功能的时候,保证不要一次性地,同一个短时间内,发出太多这样的翻译请求,把大量的翻译请求,分开来做,就可以了。
转载请注明:在路上 » 【已解决】python执行出错:HTTPError: HTTP Error 502: Bad Gateway