我有两个我不明白的问题。
首先,我偶尔会得到一个' http.client.BadStatusLine' httpconnection.request后出错。 99.99%的时间代码工作,但偶尔会返回此错误。那么这是我的代码,还是导致问题的服务器?
第二个是我在代码中有两个异常处理程序。第一个我绕过整个子程序来捕获我可能做的任何编码错误(应用程序运行无头,所以我想捕获任何错误并以这样的方式处理它们,以便我可以记录错误并终止应用程序整洁的时尚)。第二个是围绕实际的请求,所以,再次,我可以捕获任何错误,报告它们,在这种情况下,让应用程序继续。奇怪的是,它是子程序异常处理程序,它捕获' http.client.BadStatusLine'而不是请求周围的异常处理程序。这是为什么?
我希望(以及我下面的代码片段)都有意义以及任何想法,建议或方法,以确保我捕获' http.client.BadStatusLine'作为一个可恢复的错误,而不是致命的错误,将非常感激地接受!!
请原谅我的编码。我是旧学校,并没有真正掌握新的编码方式,所以我的大部分时间就像是在写一个旧的Fortran或VB程序!
谢谢,吉姆
代码如下:
try:
headers = {'X-Pvoutput-Apikey' : cArr[pvo_key][cVal],
'X-Pvoutput-SystemId' : cArr[pvo_sysid][cVal],
"Accept" : "text/plain",
"Content-type": "application/x-www-form-urlencoded"}
pvAddBaURL = "/service/r2/addbatchstatus.jsp"
try:
conn = http.client.HTTPConnection(pvo_host, timeout=ConTimeOut)
#conn.set_debuglevel(2) # debug purposes only
conn.request("POST", pvAddBaURL, "data=" + pvAddBaPrm, headers)
except:
errorReturn = sys.exc_info()
writeLog(logWn, codeUpl, "Warning > Batch upload connect unsuccessful")
writeLog(logEr, codeUpl, "Bat > Batch upload unsuccessful with:")
for errorVal in errorReturn:
writeLog(logEr, codeUpl, "Bat > Connect error: " + str(errorVal))
conn.close()
else:
response = conn.getresponse()
if response.status != 200:
writeLog(logWn, codeUpl, "Error > Batch upload unsuccessful")
writeLog(logEr, codeUpl, "Bat > Batch upload unsuccessful with:")
writeLog(logEr, codeUpl, "Bat > Resp status: " + str(response.status))
writeLog(logEr, codeUpl, "Bat > Resp reason: " + str(response.reason))
writeLog(logEr, codeUpl, "Bat > Resp read: " + str(response.read()))
conn.close()
else:
writeLog(logVb, codeUpl, "Bat > Upload successful")
writeLog(logFu, codeUpl, "Bat > Resp status: " + str(response.status))
writeLog(logFu, codeUpl, "Bat > Resp reason: " + str(response.reason))
writeLog(logDb, codeUpl, "Bat > Resp read: " + str(response.read()))
conn.close()
except:
eespvo_Fatal(sys.exc_info(), codeUpl) #fatal exception handler routine
#Routine to handle Crash!
#------------------------
#Ensures that even if there is a programming error the app can report it in the log.
def eespvo_Fatal(crashData, mod):
writeLog(logWn, codeFaE, "Fatal > eespvo (ProcId: "
+ str(eespvo_pid) +") terminated - restart attempted")
writeLog(logFa, codeFaE, "Ftl > Fatal: eespvo crashed (ProcId: "
+ str(eespvo_pid) + ", Module: " + mod)
for crashVal in crashData:
writeLog(logFa, codeFaE, "Ftl > Fatal: " + str(crashVal))
for frame in traceback.extract_tb(crashData[2]):
fname, lineno, fn, text = frame
writeLog(logFa, codeFaE, "Ftl > Fatal: <traceback line: " + str(lineno) + ">")
writeLog(logFa, codeFaE, "Ftl > Fatal: <traceback module: " + str(fn) + ">")
writeLog(logFa, codeFaE, "Ftl > Fatal: <traceback text: " + str(text) + ">")
writeLog(logFa, codeFaE, "Ftl > Fatal: eespvo (ProcId: " + str(eespvo_pid)
+") killed")
cArr[runOK][cVal] = False
#kill our own process
os.kill(eespvo_pid,signal.SIGKILL)
我的日志输出如下所示:
从子例程异常处理程序:
20141216 084503 X: FaE - Ftl > Fatal: eespvo crashed (ProcId: 2380, Module: Upl
20141216 084503 X: FaE - Ftl > Fatal: <class 'http.client.BadStatusLine'>
20141216 084503 X: FaE - Ftl > Fatal: ''
20141216 084503 X: FaE - Ftl > Fatal: <traceback object at 0xe490d0>
20141216 084503 X: FaE - Ftl > Fatal: <traceback line: 316>
20141216 084503 X: FaE - Ftl > Fatal: <traceback module: _read_status>
20141216 084503 X: FaE - Ftl > Fatal: <traceback text: raise BadStatusLine(line)>
20141216 084503 X: FaE - Ftl > Fatal: eespvo (ProcId: 2380) killed
答案 0 :(得分:0)
我仍然不知道问题1的答案。但我想我可能已经解决了问题2.我认为问题在于它是声明:
response = conn.getresponse()
失败了。在请求异常处理之外,它无法处理子例程异常。因此,解决方案是将其置于请求异常处理中,即:
try:
conn = http.client.HTTPConnection(pvo_host, timeout=ConTimeOut)
#conn.set_debuglevel(2) # debug purposes only
conn.request("POST", pvAddBaURL, "data=" + pvAddBaPrm, headers)
response = conn.getresponse()
except:
只有时间会证明!